Search code examples
phproutesargumentslaravel-5.3url-parameters

Laravel web route variable is not returning as supposed


I have the following route :

    Route::get('/web/{cat_id?}/{post_type}','WebPostsController@index')
->where('post_type', '(pictures|videos|links|companies)')
->name('post_index');

Controller file :

 public function index($post_type,$post_id = null,$cat_id = null)
    {

        dd($post_type);
}

When access the URL /public/web/15/videos It return '15' which is supposed to return {post_type} value (videos) while it returning the {cat_id?} value.


Solution

  • The variables passed in to the action have to follow the definition in the route.
    That is, in your case, you expect the cat_id first, then the post_type which means that you will have to define the method as:

    public function index($cat_id, $post_type) { }
    

    Furthermore, I expect that you will have some issues with it still, cause you can not have an optional parameter and then a forced parameter. So either move the $cat_id param as a optional to the end of the route and use $post_type before (/public/web/videos/15) or make both forced.