首页 > 解决方案 > 将两个变量传递给 Laravel 中的方法

问题描述

我也想在 url 中找到 slug 的帖子.. 但评论必须由 post_id 找到

控制器

public function post($slug,$id)
{
    $post = Post::where('slug',$slug)->first();
    $comments = Comment::where('post_id',$id)->get();
    return view('content.post',compact('post','comments'));
}

路线

Route::get('post/{slug}', 'PagesController@post')->name('post.show');

标签: mysqllaravellaravel-5laravel-6laravel-6.2

解决方案


Route::get('post/{slug}', 'PagesController@post')->name('post.show');
public function post($slug)
{
    $post = Post::where('slug',$slug)->first();
    $comments = Comment::where('post_id',$post->id)->get();
    return view('content.post',compact('post','comments'));
}

推荐阅读