Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Route model binding (slug) only works for show method?

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:

Rote 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'));
}
like image 548
PeraMika Avatar asked Dec 01 '22 12:12

PeraMika


2 Answers

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();
});
like image 56
Sumit Wadhwa Avatar answered Dec 06 '22 00:12

Sumit Wadhwa


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

like image 22
MarketHubb Avatar answered Dec 06 '22 00:12

MarketHubb