Laravel正确设置了子弹

时间:2019-02-10 07:50:06

标签: php laravel

在我的帖子模型中,我有这个:

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

当我转到页面发布时,我的链接是:http://example.com/post/there-a-slug

我想要如何链接:http://example.com/post/id_post/there-a-slug

在我的路线上:

Route::get('/post/{post}', 'PostController@show')->name('showpost');

1 个答案:

答案 0 :(得分:0)

您的路线如下所示:

Route::get('/post/{id}/{slug?}', 'PostController@show')->name('showpost');

而且您还必须在show上更新PostController函数

public function show(Request $request,$id,$slug=null)
{
  //now you can find your post with id
  $post=Post::findOrFail($id);
  //your code goes here
}