montrealinthefall's avatar

How to pass link variable to route?

I have a route for loading a checkout page, and there is a spot in the route that requires an id for the plan that is being selected to pay for. On my pricing page I have 2 plans listed, and each one has an id that I can insert into the link via {{ $plan->plan_id }} so I can create a new attribute(?) in the link such as <a href="#" plan="{{ $plan->plan_id }}"> or something like that, or I can simply pass it through the url, which is not preferred, but also an option, such as <a href="/{{ $plan->plan_id }}> . Or obviously a better option if available.

Any suggestions on how to pass this data to the route listed below would be much appreciated.

Route::get('/subscribe', function (Request $request) {
    return $request->user()->subscribe('(plan-id)');  
});

thank you

0 likes
6 replies
tykus's avatar

If you are intending to use a GET request, then your options are a URL segment domain.tld/subscribe/{plan} or a query parameter domain.tld/subscribe?plan={plan}. And your markup will be written accordingly

<a href="/subscribe/{{ $plan->plan_id }}">Subscribe</a>
<!-- or -->
<a href="/subscribe?plan={{ $plan->plan_id }}">Subscribe</a>

Otherwise, if you want to effectively put the Plan into a Cart (in the Session); you could POST a form instead; then redirect the checkout page.

montrealinthefall's avatar

@tykus Thanks for the reply. So I am not confused about the link mark up necessarily, but moreso how to retrieve that plan id in the route, because right now I just have created 2 routes, 1 for each plan and I've manually inserted the id myself into the route, like this

Route::get('/subscribe', function (Request $request) {
    return $request->user()->subscribe('(1523484)');  
});

But I would prefer to just be able to just have 1 route with a variable for the plan id that I can pass to the route somehow. I just can't figure out how to get this data to the route. For example:

Route::get('/subscribe', function (Request $request) {
    return $request->user()->subscribe('($plan-id)');  
});

And then each link will send the proper id to the checkout function.

I also meant to link to the function in the controller, but I was struggling to figure out which was the one I needed. this one is huge, so sorry for the wall of text, but I think this is the function that is being called with that route. If I'm correct, perhaps the id could be passed here?

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' => $this->variant,
                        ],
                    ],
                ],
            ],
        ]);

Links that might help:

Payment processor docs for this specific checkout function https://github.com/lmsqueezy/laravel#creating-subscriptions

file that contains that giant function that I'm not 100% positive is the one I need for this: https://github.com/lmsqueezy/laravel/blob/main/src/Checkout.php

tykus's avatar

@montrealinthefall okay, well one Route definition will look like this (using a URL segment and Route Model binding)

Route::get('/subscribe/{plan}', function (Request $request, Plan $plan) {
    return $request->user()->subscribe($plan->id);  
});

or, if you prefer a query parameter plan

Route::get('/subscribe', function (Request $request) {
    return $request->user()->subscribe($request->get('plan'));  
});
montrealinthefall's avatar

@tykus The second is much more ideal, but I am struggling to figure out actually how to get 'plan' to this function now. Sorry, I am very new to laravel and everything is a bit confusing. I feel like there are a number of ways for me to insert this plan id into the url from my pricing page, but none of them are working.

Can you recommend the way to put this data into the url if I am using this route?

Route::get('/subscribe', function (Request $request) {
    return $request->user()->subscribe($request->get('plan'));  
});

thank you again, and pardon my ignorance.

Please or to participate in this conversation.