Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

princelionelnzi's avatar

Handle "No query results for model" from model binding

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');
});
0 likes
4 replies
mushketer888's avatar

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.

mushketer888's avatar

You have ThreadsController@index on both

threads

and

threads/{channel}

You can try:


public function index(Channel $channel=null)
//or public function index($channel=null)
    {
    //$channel=Channel::find($channel);
        if (!is_null($channel)) {//or $channel->exists
            $threads = $channel->threads()->latest()->get();
        } else {
            $threads = Thread::latest()->get();
        }
        return view('threads.index', compact('threads'));
    }

I haven't tested it! Just idea

mushketer888's avatar

Example:

For model-focused call function (User $user):
Existing User instance if the User is found. ✔
Empty User model if User is skipped (when we hit /people, but let's don't hit people, people). ✔
ModelNotFoundException is thrown if User doesn't exist. ✘
For instance-focused call function (User $user = null):
Existing User instance if the User is found. ✔
Empty User model if User is skipped. ✘
Empty User model if User doesn't exist. ✘

Please or to participate in this conversation.