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

Arpit's avatar
Level 10

No query results for model [App\Tags]

I am trying to fetch list of tags, but i keep getting this error :

NotFoundHttpException in Handler.php line 131: No query results for model [App\Tag]

Here is my routes file

<?php


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

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

Route::get('/browse', 'TeamsController@index');
Route::get('/teams/create', 'TeamsController@create');
Route::post('/teams/create', 'TeamsController@store')->name('browse');

Route::get('/teams/tags/{tag}', 'TagsController@index')->name('tagsIndex');

Auth::routes();

Route::get('/home', 'HomeController@index');

My Tag model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Team;

class Tag extends Model
{
    public function teams()
    {
        return $this->belongsToMany(Team::class);   
    }

    public function getRouteKeyName()
    {
        return 'name';  
    }
}

My TagsController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Tag;

class TagsController extends Controller
{
    public function index(Tag $tag)
    {
        $tags = $tag->load('teams');

        return view('team.index', compact('tags'));
    }
}

Why it happens? Whats is the solution of this problem? Any help would be great!

0 likes
2 replies
tykus's avatar

Assuming you are visiting your /teams/tags/{tag} URI, it means that whatever tag id you are giving to the index method does not exist.

If you were not passing an id but instead wanted to use a tag name for route model binding (where name is a column having unique values), you will need to update your model

public function getRouteKeyName()
{
    return 'name';
}
Arpit's avatar
Level 10

So the problem was that my Tags table is empty (sorry, my bad).

Still thanks @tykus (i am sure this won't be the only time i need ur help)

Please or to participate in this conversation.