Laravel Route 模型绑定(slug)仅适用于 show 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37991994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:03:40  来源:igfitidea点击:

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

phplaravelrouteslaravel-5.2slug

提问by PeraMika

I have Articlemodel and articlestable in the database. Each article can be shown using Laravel's standard URI structure: www.example.com/articles/5(where 5is the article id). Each article has a slugfield (slugcolumn in the articles table), so with Route Model BindingI use sluginstead of id:

我在数据库中有文章模型和文章表。每篇文章都可以使用 Laravel 的标准 URI 结构显示:(文章www.example.com/articles/5在哪里)。每篇文章都有一个slug字段(文章表中的slug列),因此我使用路由模型绑定而不是:5idslugid

RouteServiceProvider.php:

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:

在routes.php我有:

Route::resource('articles', 'ArticleController');

and now articles can be accessed with URLs like: www.example.com/some_slug.

现在可以使用 URL 访问文章,例如: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].

没有模型 [App\Article] 的查询结果。

For example, when I try to open the following: www.example.com/some_slug/edit- I get that error.

例如,当我尝试打开以下内容时:www.example.com/some_slug/edit- 我收到该错误。

So, method ArticleController@show(Article $article) works fine, but ArticleController@edit(Article $article) doesn't work.

因此,方法 ArticleController@show(Article $article) 工作正常,但 ArticleController@edit(Article $article) 不起作用。

Here is my route list:

这是我的路线清单:

Rote List

死记硬背

and here are showand edit methods from ArticleController:

这里是来自 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'));
}

回答by MarketHubb

I believe in 5.2, you can customize the key name directly in the model using the getRouteKeyNamemethod:

相信在5.2中,可以直接在model中使用getRouteKeyName方法自定义键名:

public function getRouteKeyName()
{
    return 'slug';
}

docs

文档

回答by swadhwa

If anyone wants to do route model binding with both idand slug, explicitly binding like this works:

如果有人想同时使用idand进行路由模型绑定slug,那么显式绑定是这样工作的:

Route::bind('product', function($value) {
   return \App\Product::where('id', $value)->orWhere('slug', $value)->first();
});

回答by Javier Gonzalez

I have another approach for you without touching RouteServiceProvider.php. I hope it helps.
You need use App\Article;and use App\Tag;in your ArticleController.
ArticleController

我有另一种方法,无需触及 RouteServiceProvider.php。我希望它有帮助。
您需要使用 App\Article; 使用 App\Tag; 在您的文章控制器中。
文章控制器

public function show($slug)
    {
        $article = Article::where('slug', $slug)->first();

        $tags = Tag::lists('name', 'id');

        return view('articles.edit', compact('article', 'tags'));
    }

public function edit($slug)
    { 
        $article = Article::where('slug', $slug)->first();

        $tags = Tag::lists('name', 'id');

        return view('articles.edit', compact('article', 'tags'));
    }