I have a variable in a Laravel 5 controller which I am trying to include in the url of a redirect link, however nothing I've tried seems to work, my code is currently as follows.

$newChallenge = ((int) request('challenge_id') + 2);

return redirect('/texttext/{{$newChallenge}}');

However, this doesn't use the variable represented by $newChallenge, but rather
"{{newChallenge}}" as a string, how should this be done?

2 Answers

1
FunkyMonk91 On Best Solutions

{{ }} is meant for use in blade files.

I think you can do return redirect('/texttext/{ $newChallenge }'); if you really want those { } brackets.

Or just return redirect(sprintf('/texttext/%s'), $newChallenge));

or return redirect('/texttext/'.$newChallenge);

Edit: Also, your redirect could probably benefit from being a named route. https://laravel.com/docs/5.8/routing#named-routes

0
Prafulla Kumar Sahu On

May be you can change it to a query parameter

like

return redirect('/texttext/?=newChallenge={{$newChallenge}}');

if you want to use

return redirect('/texttext/{{$newChallenge}}');

It should be something like below

Route::get('texttext/{{newChallenge}}', '[email protected]');

and newChallenge signature will function newChallenge(Request $request, $newChallenge)

if you want to use something like return redirect(route('new-challenge', $newChallenge));

just add name to route like

Route::get('texttext/{{newChallenge}}', '[email protected]')->name('new-challenge');

I have not tested, but I think it should work.