In this Laravel 9 route optional parameter tutorial, I will show you how to pass optional parameters in Laravel, and also we will see how to pass the default values with this optional route parameter.

To pass the optional parameter, we have to pass the ? sign after the parameter name like:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/user/{name?}', function ($name = null) {
    return $name;
});

 

Now let's see how to pass laravel route optional parameter default value:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/user/{name?}', function ($name = 'John') {
    return $name;
});

 

Read also: Laravel 9 Route With Parameter Example

 

Hope it can help you.

Category : #laravel

Tags : #laravel , #laravel routing