return(); doesn't work for 1 route but works for almost the same route
P粉098417223
P粉098417223 2024-04-04 17:34:09
0
1
305

I have 2 routes, one for unsubscribing and one for restoring, both routes are the same except for the name/function/url unless I'm missing something. When I cancel the resume subscription, I click the /resume url, it performs the function and then comes back, everything is so fast and never seems to leave the page, then it flashes the success message.

My /cancel url just goes to a blank page (I'm pretty sure this is correct because if it was working properly you would never see it) and it performs the cancel function but never returns. When I go back manually using the back button it flashes a success message. Just don't understand why it doesn't return on its own as expected. Apart from that, if you need any other information, please let me know.

Work:

Route::get('/resume', function (Request $request) {
// $user = User::find(1);
$user = Auth::user();
$response = $user->subscription()->resume();
toastr()->success('Your subscription has been successfully resumed.', 'Success' , ['closeButton' => 'true','positionClass' => 'toast-top-center']);
return back();
});

Not working:

Route::get('/cancel', function (Request $request) {
// $user = User::find(1);
$user = Auth::user();
$response = $user->subscription()->cancel();
toastr()->success('Your subscription has been successfully cancelled.', 'Success' , ['closeButton' => 'true','positionClass' => 'toast-top-center']);
return back();
});

I don't think these are necessary, but just in case, here are the 2 functions in my subscription controller.

public function cancel(): self
    {
        $response = LemonSqueezy::api('DELETE', "subscriptions/{$this->lemon_squeezy_id}");

        $this->sync($response['data']['attributes']);

        return $this;
    }

    /**
     * Resume the subscription.
     */
    public function resume(): self
    {
        if ($this->expired()) {
            throw new LogicException('Cannot resume an expired subscription.');
        }

        $response = LemonSqueezy::api('PATCH', "subscriptions/{$this->lemon_squeezy_id}", [
            'data' => [
                'type' => 'subscriptions',
                'id' => $this->lemon_squeezy_id,
                'attributes' => [
                    'cancelled' => false,
                ],
            ],
        ]);

        $this->sync($response['data']['attributes']);

        return $this;
    }

P粉098417223
P粉098417223

reply all(1)
P粉231112437

Resolved by commenting on this thread https://laracasts.com/discuss/channels/laravel/redirect-with-not-working?page=1&replyId=337923

I did a manual redirect but that didn't work either so I added ->send(); to the end as this thread suggested and that solved it. I didn't test this with return back(); but it might work too.

The successful code is as follows

return redirect()->to('/settings/subscription')->send();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!