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

princeoo7's avatar

Route Model Implicit binding not working for me.

Source: https://laravel.com/docs/5.8/routing#route-model-binding

I was trying laravel from scratch ep 30 and 31 and found jeffery doing implicit binding of route but when i tried i get null result. why so ?

Tag model :

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

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

Tag Controller

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

class TagsController extends Controller
{
    public function index(Tag $tag)
        {
            return $tag;
        }
}

Route

Route::get('/blog/tag/{name}', 'TagsController@index')->name('blogs.showbyTag');

whats wrong in my code ?

0 likes
11 replies
Sergiu17's avatar

And your route?

Route::get('/blog/tag/{tag}', 'TagsController@index')->name('blogs.showbyTag');

Route wildcard should match with argument from your function

public function index(Tag $tag)

1 like
princeoo7's avatar

@SERGIU17 - Oh i see, i thought that the route can have any name for the param. my bad.

one slight request i have, i am trying to get the reverse relation for articles.

i got that but how to use paginate($limit) on the code below ?

public function index(Tag $tag)
{
    
    $records = $tag->articles;

    return view('pages.frontend.articles.byTags', compact('records'));
}

and if i use below code i get:

$records = $tag::with('articles')->paginate(20);

    return dd($records->articles);

error :

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$articles

i have to use another view just for testing as the main view have $records->link() and it will be undefined if i am not using pagination here.

i am stuck here from yesterday :(

princeoo7's avatar

@SERGIU17 updated my answer previously as i solved the prior issue and stuck in new one :(

Sergiu17's avatar

@PRINCEOO7 - try this

public function index(Tag $tag)
{
    $records = $tag->load('articles');

    return view('pages.frontend.articles.byTags', compact('records'));
}
princeoo7's avatar

@SERGIU17 - I get the result either with load() or with() but anything about having pagination ?

i still get:

error

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$articles

on:

$tag->load('articles')->paginate(20);
//or
$tag::with('articles')->paginate(20);

Sergiu17's avatar

@PRINCEOO7 - this should work

$tag::with(['articles' => function($query) {
    $query->paginate(20);
]);
1 like
princeoo7's avatar

@SERGIU17 -

$records = $tag::with([
        'articles' => function ($query) {
            $query->paginate(20);
        }
    ]);

code above gave me, i think builder query :

Builder {#372 ▼
#query: Builder {#366 ▶}
#model: Tag {#373 ▶}
#eagerLoad: array:1 [▶]
#localMacros: []
#onDelete: null
#passthru: array:13 [▶]
#scopes: []
#removedScopes: []
}

attaching ->get(); at the end gave me below despite of passing a tag name as a param. to the url : http://localhost:3000/blog/tag/quia

$records = $tag::with([
        'articles' => function ($query) {
            $query->paginate(20);
        }
    ])->get();

Result:

Collection {#397 ▼
#items: array:25 [▼
 0 => Tag {#403 ▶}
    1 => Tag {#404 ▶}
    2 => Tag {#405 ▶}
    3 => Tag {#406 ▶}
    4 => Tag {#407 ▶}
    5 => Tag {#408 ▶}
    6 => Tag {#409 ▶}
    7 => Tag {#410 ▶}
    8 => Tag {#411 ▶}
    9 => Tag {#412 ▶}
    10 => Tag {#413 ▶}
    11 => Tag {#414 ▶}
    12 => Tag {#415 ▶}
    13 => Tag {#416 ▶}
    14 => Tag {#417 ▶}
    15 => Tag {#418 ▶}
    16 => Tag {#419 ▶}
    17 => Tag {#420 ▶}
    18 => Tag {#421 ▶}
    19 => Tag {#422 ▶}
    20 => Tag {#423 ▶}
    21 => Tag {#424 ▶}
    22 => Tag {#425 ▶}
    23 => Tag {#426 ▶}
    24 => Tag {#427 ▶}
]
}

and even if i try doing / accessing the articles as followed, i get:

public function index(Tag $tag)
{
    $records = $tag::with([
        'articles' => function ($query) {
            $query->paginate(20);
        }
    ])->get();

    return dd($records->articles);

}

i get error:

Property [articles] does not exist on this collection instance.

Snapey's avatar
Snapey
Best Answer
Level 122

it should be

$records = $tag->articles()->paginate();
2 likes
Snapey's avatar

Please mark it solved (if it is)

You should mark the answer for the original question

Please or to participate in this conversation.