How to pass default parameters to laravel controller in a get route

25,232

Register it as a separate route:

Route::get('/', 'Controller@getCategory')->named('home');
Route::get('category/{id}/{date?}', 'Controller@getCategory')->named('category/date');

Then in your controller, set default values for those arguments:

public function getCategory($id = 1, $date = '2015-12-18')
{
    // do your magic...
}
Share:
25,232

Related videos on Youtube

horse
Author by

horse

Updated on December 03, 2020

Comments

  • horse
    horse over 3 years

    I have a route like that:

    Route::get('category/{id}/{date?}', array('as' => 'category/date', 'uses' => 'Controller@getCategory'));
    

    I want to run @getCategory with default parameters when called '/' root route. So if '/' route called, getCategory function should run with id=1 and date=2015-12-18.

    How should I do that?

  • Robert
    Robert over 7 years
    Of course it is one way to do this. But is there any way to do this in one route and not in controller?
  • emotality
    emotality almost 7 years
    Id also like to know how to pass static parameter in route. Can't find answer anywhere. :(
  • Yako
    Yako about 5 years
    What is the solution if you want to use different default values according to the route pattern?
  • ryandi pratama purba
    ryandi pratama purba over 2 years
    how do i access the endpoint without id