I guess its because you use injection
index(Channel $channel)
Laravel doing your code
if ($channel->exists) {
automatically here.
As I know if model is not found it reports 404.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am following the series Let's Build A Forum with Laravel and TDD (https://laracasts.com/series/lets-build-a-forum-with-laravel).
I want to filter my threads by the channel.
To show a thread I go to forum.dev/threads/channelSlug/threadID
To view all the threads of a particular channel, I visit forum.dev/threads/channelSlug
The problem is that anytime I try viewing all the threads of a particular channel, I get the expected results if channelSlug`` is correct but when I enter an incorrectchannelSlugI get a404 page (No query results for model [App\Channel].)`` instead of displaying all the threads as specified in the controller below
This is my controller
public function index(Channel $channel)
{
if ($channel->exists) {
$threads = $channel->threads()->latest()->get();
} else {
$threads = Thread::latest()->get();
}
return view('threads.index', compact('threads'));
}
This is my routes
// Threads
Route::group(['prefix' => 'threads', 'as' => 'threads.'], function () {
Route::get('/', 'ThreadsController@index')->name('index');
Route::get('create', 'ThreadsController@create')->name('create');
Route::post('/', 'ThreadsController@store')->name('store');
Route::group(['prefix' => '{channel}'], function () {
Route::get('/', 'ThreadsController@index');
Route::get('{thread}', 'ThreadsController@show')->name('show');
Route::get('{thread}/edit', 'ThreadsController@edit')->name('edit');
Route::put('{thread}/edit', 'ThreadsController@update')->name('update');
Route::delete('{thread}/delete', 'ThreadsController@destroy')->name('destroy');
});
Route::post('{thread}/replies', 'RepliesController@store')->name('addReply');
});
Please or to participate in this conversation.