Skip to content
Advertisement

Get Laravel route with optional parameter

I was wondering if it’s possible to get the full url/path in Laravel with a optional parameter in it.

In web.php I have this:

Route::get('/blabla/{var1}/blabla/{var2?}', [Controller::class, 'function'])->name('myroute');

And want to get the full url/path with this:

echo route('myroute', 'var1', 'var2');

But only ‘var1’ appears. How come? In the manual there are no examples.

Advertisement

Answer

The parameters should be in an array like:

echo route('myroute', ['var1', 'var2']);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement