Pass plan_id to route and/or function?
I have LemonsSqueezy 99% configured and working properly, but I am on the very last step right now trying to figure out how to have 1 single route and pass the plan id to it depending on which link I click. Here is my route for this in web.php
Route::get('/subscribe', function (Request $request) {
return $request->user()->subscribe(plan-id-here);
return view('wave.pricing', ['subscribe' => $subscribe]);
});
and I just need to pass the same data that {{ $plan->plan_id }} would do elsewhere, to insert it into subscribe(plan-id-here);
. Presumably I should put this in my url for each plan on my pricing page, but should I add an attribute like <a href="#" plan="{{ $plan->plan_id }}">
or something like this? Or pass through the url somehow? like <a href="subscribe?plan={{ $plan->plan_id }}
. I wasn't sure if I would need to pass this to the route or the function in the controller, so here is that function (I think it's the right one..) let me know if you have any good suggestions on how and where to pass the plan_id to that route. Thank you so much.
public function url(): string
{
$response = LemonSqueezy::api('POST', 'checkouts', [
'data' => [
'type' => 'checkouts',
'attributes' => [
'custom_price' => $this->customPrice,
'checkout_data' => array_merge(
array_filter($this->checkoutData, fn ($value) => $value !== ''),
['custom' => $this->custom]
),
'checkout_options' => array_filter([
'embed' => $this->embed,
'logo' => $this->logo,
'media' => $this->media,
'desc' => $this->desc,
'discount' => $this->discount,
'dark' => $this->dark,
'subscription_preview' => $this->subscriptionPreview,
'button_color' => $this->buttonColor ?? null,
], function ($value) {
return ! is_null($value);
}),
'product_options' => [
'redirect_url' => $this->redirectUrl ?? config('lemon-squeezy.redirect_url'),
],
'expires_at' => isset($this->expiresAt) ? $this->expiresAt->format(DateTimeInterface::ATOM) : null,
],
'relationships' => [
'store' => [
'data' => [
'type' => 'stores',
'id' => $this->store,
],
],
'variant' => [
'data' => [
'type' => 'variants',
'id' => $plan->plan_id,
],
],
],
],
]);
Hi there!
One way to do this would be to pass the plan_id
as a query parameter. Here's how you could do that:
Update your route to handle a query parameter for the plan_id
:
Route::get('/subscribe', function (Request $request) {
$planId = $request->query('plan');
$request->user()->subscribe($planId);
return view('wave.pricing', ['subscribe' => $subscribe]);
});
In this setup, we are using Laravel's $request->query() to retrieve the plan_id from the query parameters of the URL.
Update your link to include the plan_id as a query parameter:
<a href="/subscribe?plan={{ $plan->plan_id }}">Subscribe</a>
This way, when the user clicks on the link, the plan_id
will be passed as a query parameter in the URL.
The rest of the steps remain the same as my previous response. Make sure to correctly reference $plan->plan_id
in your url()
function and update your subscribe method in your User model to accept the plan_id
parameter and use it accordingly.
You would still need to handle the issue of having two return statements in your route function. You need to adjust the logic so that both lines of code can be executed as intended. It's likely you need to subscribe the user and then return the view, or adjust based on your specific needs.
Hope that this helps!
That's great, thank you so much, it works perfectly. I only had to make one modification:
$request->user()->subscribe($planId);
->
return $request->user()->subscribe($planId);
Otherwise it works great. Thank you for your help.
I am up and running with Lemon Squeezy, and I'll do a little write up in a few days in case there's anyone else that can maybe benefit from it, because I certainly prefer like every single thing about it over Paddle.
the only thing I didn't get done was the new checkout function changing the user role in wave settings, but the lemon squeezy subscriber table has enough in there so that I don't really need this functionality. I'm sure you could fix that in 5 minutes before you post the write up if you want, but otherwise it's kind of okay without it.
thanks again for all your help.
edit: The roles/voyager issue is not solvable at the moment, as voyager isn't compatible with Laravel 10 yet. but it seems to work fine as it is (aside from the missing plan slug setting) but you just can't make any changes at the moment. Not a big deal.















