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

rapliandras's avatar

Route model binding with slug instead of id

What I want is the route model binding functionality (accessing Strategy $strategy as a method argument) for custom routes which use title slugs rather than ids.

I have this in RSP

        Route::bind('strategy', function($value) {
            return \App\Article\Strategy::where('slug', $value)->first();
        });

and this in my routes file:

Route::get(\Lang::get('slugs.strategy').'/{strategy}', 'ArticleController@showStrategy');

It simply does not work. RouteServiceProvider is loaded in app.php, yet I cannot dump anything in it. Accessing Strategy $strategy results in an empty skeleton object. What's going on here?

// @JeffreyWay, pressing altgr in the laracasts editor does not work, please fix it

0 likes
9 replies
bobbybouwmann's avatar

What happens when you try this?

Route::bind('strategy', function ($value) 
{
    dd($value); // Check if the value is correct
    // return \App\Article\Strategy::where('slug', $value)->first();
});
bobbybouwmann's avatar

Then the binding of route is not working! Does it show any result when you change it back to look for an id?

anouarabsslm's avatar

could you try this:

$router->bind('strategy', function($value) {
            return \App\Article\Strategy::whereSlug( $value)->first();
 });
1 like
rapliandras's avatar

config/app.php

providers => [
// ...
        'App\Providers\RouteServiceProvider',
];

yet nothing works, not even a die

rapliandras's avatar
rapliandras
OP
Best Answer
Level 3

A composer update from 5.0.0. to 5.0.2. solves the problem.

Please or to participate in this conversation.