1

I would like to define {id} an optional parameter in the following route:

Route::get('profile/edit/{id}', array('uses' => 'BusinessController@editProfile', 'as' => 'profile.edit'));

How can I do this and also define a default parameter if one isn't provided?

3 Answers 3

1

Just like the other answers, but as for the default part: I asked almost the same question a few days ago and the answer is:

Route::get('profile/edit/{id?}', array('uses' => 'BusinessController@editProfile', 'as' => 'profile.edit'))
    ->defaults('id', 'defaultValue');

The important things are

  • The questionmark
  • The defaults function
Sign up to request clarification or add additional context in comments.

Comments

0
Route::get('profile/edit/{id?}', array('uses' => 'BusinessController@editProfile', 'as' => 'profile.edit'));

you can pass {id?} for optional parameters in your route.

Laravel will take it as a optional. it is called as a wild card in laravel

Comments

0

Just put a question mark after it in the route, and give it a default in the function:

Route::get('profile/edit/{id?}', ...
public function editProfile ($id = 123) { ... }

Documentation: https://laravel.com/docs/5.4/routing#parameters-optional-parameters

Comments

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.