This is the problem
<a href="{{ route('test',$post)}}">
Laravel doesn't know it needs to provide the slug here as the key for the route. Try this instead
<a href="{{ route('test', $post->slug) }}">
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone !
I have a problem with my route. When I put the slug in parameter I get a 404 error
Knowing that in my database the slug field is unique, not null, not empty
My Model
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
My Controller
public function test(Post $post)
{
return view('visit.one-post', compact('post'));
}
My route
Route::get('/posts/{post}',[PostController::class, 'test'], function () {
return view('visit.one-post);
})->name('test');
My view
<a href="{{ route('test',$post)}}">
<button class="btn btn-info" type="submit">See post</button>
</a>
When I delete the line below my route uses id as a parameter and it works I return my view with the correct data
public function getRouteKeyName ()
{
return 'slug';
}
So I come to you because I don't know where my mistake came from Thank you in advance I wish you all a good day: D
@Userinfo look If you want to use the root binding model method, you must use this method in your method
public function test(Post $post)
{
return view('test', compact('post'));
}
// this method use default id for fetch data from post table
// route example for read single post - Route::get('posts/{post}', [postsController::class, 'test'])->name('posts.single');
// now how to fetch view - <a href="{{ route('posts.single', $post->id) }}">mor info</a>
if you want use route model binding for slug, you must this methods
public function getRouteKeyName()
{
return 'slug';
}
// this method must be write post model
// route example for read single post with slug - Route::get('posts/{post}', [postsController::class, 'test'])->name('posts.single');
// now how to fetch view - <a href="{{ route('posts.single', $post->slug) }}">mor info</a>
edit with id without route model binding
public function edit($id)
{
$post = Post::where('id', $id)->first();
return view('test', compact('post'));
}
Route::get('posts/{anything}', [postsController::class, 'edit'])->name('posts.edit);
// href into view - <a href="{{ route('posts.edit', $post->id) }}">edit</a>
edit with id with route model binding
public function edit(Post $post)
{
return view('test', compact('post'));
}
Route::get('posts/{post}', [postsController::class, 'edit'])->name('posts.edit);
// href into view - <a href="{{ route('posts.edit', $post->id) }}">edit</a>
edit with slug without route model binding
public function edit($slug)
{
$post = Post::where('slug', $slug)->first();
return view('test', compact('post'));
}
Route::get('posts/{anything}', [postsController::class, 'edit'])->name('posts.edit);
// href into view - <a href="{{ route('posts.edit', $post->slug) }}">edit</a>
edit with slug with route model binding
public function edit(Post $post)
{
return view('test', compact('post'));
}
Route::get('posts/{post}', [postsController::class, 'edit'])->name('posts.edit);
// href into view - <a href="{{ route('posts.edit', $post->slug) }}">edit</a>
// And I emphasize that you should use this structure in the model except for the ID with anything else that you want to read the post
For example, here I want to read the post using the slag column
public function getRouteKeyName()
{
return 'slug';
}
Please or to participate in this conversation.