1

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

4
  • Simply, add the optional parameters at the end of the URL, e.g. Route::get('flight/{depdate}/{from}/{to}/{ftype}/{total}/{class}/{retdate?}', 'airlineController@index'); Commented Sep 22, 2017 at 6:08
  • what if i have two optional parameters? just in case Commented Sep 22, 2017 at 6:08
  • You can add as many, but just have to take care of the sequence. Or use the POST request that you may easily take care of parameters that are optional or not set. Commented Sep 22, 2017 at 6:10
  • You can also pass the optional parameters as query string eg: <your route>?return=date&your additional optional parameters Commented Sep 22, 2017 at 6:40

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

8 Comments

what if i have tow optional parameters ? just in case
you can add fany number i think
will it be good to do so ? i mean according to the standards or something
@SaurabhGupta.as dev said you can sue post .its better choice i think
i have learnt that to fetch data one should use get but post will also do right theres no harm in it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.