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

- 1 answer

Ad

I have a route like that:

Route::get('category/{id}/{date?}', array('as' => 'category/date', 'uses' => '[email protected]etCategory'));

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?

Ad

Answer

Ad

Register it as a separate route:

Route::get('/', '[email protected]')->named('home');
Route::get('category/{id}/{date?}', '[email protected]')->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...
}
Ad
source: stackoverflow.com
Ad