I have Article model and articles table in the database. Each article can be shown using Laravel's standard URI structure: www.example.com/articles/5
(where 5
is the article id
). Each article has a slug field (slug column in the articles table), so with Route Model Binding I use slug
instead of id
:
RouteServiceProvider.php:
public function boot(Router $router)
{
parent::boot($router);
\Route::bind('articles', function($slug) {
return \App\Article::where('slug', $slug)->firstOrFail();
});
}
In routes.php I have:
Route::resource('articles', 'ArticleController');
and now articles can be accessed with URLs like: www.example.com/some_slug
.
But now, when I want to edit some article, I get the following error:
No query results for model [App\Article].
For example, when I try to open the following: www.example.com/some_slug/edit
- I get that error.
So, method ArticleController@show(Article $article) works fine, but ArticleController@edit(Article $article) doesn't work.
Here is my route list:
and here are show and edit methods from ArticleController:
public function show(Article $article) // THIS WORKS FINE
{
$tags = $article->tags()->get();
return view('articles.show', compact('article', 'tags'));
}
public function edit(Article $article) // DOESN'T WORK -> When I open article/slug/edit I get error: No query results for model [App\Article].
{
$tags = Tag::lists('name', 'id');
$categories = Category::orderBy('lft', 'asc')->get()->lists('padded_name', 'id');
return view('articles.edit', compact('article', 'tags', 'categories'));
}
If anyone wants to do route model binding with both id
and slug
, explicitly binding like this works:
// App\Providers\RouteServiceProvider::boot
Route::bind('product', function($value) {
return \App\Product::where('id', $value)->orWhere('slug', $value)->first();
});
I believe in 5.2, you can customize the key name directly in the model using the getRouteKeyName
method:
public function getRouteKeyName()
{
return 'slug';
}
docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With