how to set param optional in routes in laravel 8 code example

Example 1: route optional parameter laravel

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

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

Example 2: laravel optional params

//Use Route::get('/path/{id}/{start?}/{end?}', 'Controller@index'); 
//and handle the parameters in the controller function:

public function index($id, $start = null, $end = null)
{
    if (!$start) {
        // set start
    }

    if (!$end) {
        // set end
    }

    // do other stuff
}

Tags:

Php Example