mysql - 将两个变量传递给 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');
解决方案
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'));
}
推荐阅读
- python - Mastodon.py 注册您的应用程序是什么意思?
- ios - 为什么我在制作函数时会得到 (self: ) ?
- react-native - 未定义不是对象(评估'_native.NavigationHelperContext.Provider')
- asp.net-mvc - ASP.NET Core MVC 中的国家和城市下拉列表,采用 db-first 方法
- laravel - Eloquent hasMany 返回空集合
- esoteric-languages - 吃鸡编程你好世界
- java - 分析 Java 代码会更改执行时间
- angular - 从单个数字中删除前导零
- sql - 从具有附加连接的另一个查询结果中选择
- reactjs - `component` 属性如何与 Tab 组件一起使用?