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'));
}
s
at the end of youredit
parameter. Should beedit(Article $articles)
notedit(Article $article)
in order to match/articles/{articles}
– Jeff Puckett