Saurabh Gupta
Saurabh Gupta

Reputation: 122

Calling a laravel route with optional parameter

I have my laravel route like this

Route::get('flight/{depdate}/{from}/{to}/{ftype}/{retdate?}/{total}/{class}',
'airlineController@index');

In this case when I call this route like this it works

http://localhost:8000/flight/2017-09-20/mumbai/delhi/return/2017-09-
20/2/business

But when I keep retdate optional while calling lie the below code the route is not found

http://localhost:8000/flight/2017-09-20/mumbai/delhi/one-way/2/business

what should i do to take care of the optional parameter retdate

thankxx any help will be appreciated

Upvotes: 1

Views: 4919

Answers (1)

Vision Coderz
Vision Coderz

Reputation: 8078

You can use optional parameter

Optional Parameters Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:

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

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

Ref: https://laravel.com/docs/5.5/routing#parameters-optional-parameters

Upvotes: 2

Related Questions