dobrganch
dobrganch

Reputation: 13

Laravel route parameter not working

Any help why this is not working ,I am using Laravel 5.5.23 version ,this is are my routes :

<?php



Route::get('/', function () {
    return view('welcome');
});

Route::resource('threads','ThreadController');
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('threads','ThreadController@index');
Route::get('threads/{channel}','ThreadController@index');
Route::get('threads/create','ThreadController@create');
Route::get('threads/{channel}/{thread}','ThreadController@show');
Route::post('threads','ThreadController@store');
Route::post('/threads/{channel}/{thread}/replies','ReplyController@store');
Route::get('/logout' , 'Auth\LoginController@logout');

This is the ThreadController ,just the relevant methods actually :

public function __construct()
    {
        $this->middleware('auth')->except(['index','show']);
    }

    public function index($channel = null)
    {
        if($channel){
            //do something
        }
        else{
            $threads=Thread::latest()->get();
        }
        return view('threads.index',compact('threads'));
    }

The problem is when I try to access /threads/someChannel it returns not found ,so this is the problematic route : Route::get('threads/{channel}','ThreadController@index'); ,all other routes are working ,any idea why this one is not working ?

Upvotes: 1

Views: 1026

Answers (1)

lagbox
lagbox

Reputation: 50481

The Route::resource('threads','ThreadController') call is defining 7 routes with the prefix threads. Some of the routes you are defining yourself are being masked by this. Check php artisan route:list to see what routes the Route::resource call registers for you first. First come first serve when it comes to matching routes.

/threads/someChannel is going to match the route defined by the resource call:

GET        /threads/{thread}        ThreadController@show

Since you don't have the definition for show which is relevant to have, I would assume you have Implicit Model Binding happening. It is trying to bind that model to that parameter and it can't find it in the database by that ID and is causing a 404 because of it.

Upvotes: 1

Related Questions