laravel - 如何在使用方法选择的lavavel刀片视图中显示两个模型数据
问题描述
我已经通过 with() 方法从两个模型中选择了数据,现在我想在视图中显示这两个模型的记录,我该怎么做。
$posts=Post::where('slug','=',Str::lower($id))->with('comment')->first();
解决方案
在你的控制器中使用这个
$posts=Post::where('slug','=',Str::lower($id))->get();
return view('view.name',compact('posts');
让我们在你的刀片中试试这个
@foreach($posts as $val)
{{$val->comment()->id}}
@endforeach
推荐阅读
- c - 如何在vscode中调试多个C文件?
- python - 使用 numpy 和 jax 进行非传递子类化
- python - 创建登录和注册页面 HTML 和 CSS 后如何获取用户输入并保存到数据库?
- html - 根据对象在 Mat-Dialog 中显示不同的信息?
- javascript - 如何在 git-hook 中应用 eslint/prettier 自动修复
- office-ui-fabric - Fluent UI 组合框 allowFreeform
- shopify - 创建新订单时,Shopify 如何更新库存?
- javascript - 如何将嵌套数组的值更改为所有相同的字符串?
- git - 如何将 git clone --reference 变成独立克隆?
- c++ - 将信号连接到 lambda 时 Qt::QueuedConnection 和 Qt::DirectConnection 之间的区别