首页 > 解决方案 > 如何在使用方法选择的lavavel刀片视图中显示两个模型数据

问题描述

我已经通过 with() 方法从两个模型中选择了数据,现在我想在视图中显示这两个模型的记录,我该怎么做。

$posts=Post::where('slug','=',Str::lower($id))->with('comment')->first();

标签: laravel

解决方案


在你的控制器中使用这个

$posts=Post::where('slug','=',Str::lower($id))->get();
return view('view.name',compact('posts');

让我们在你的刀片中试试这个

@foreach($posts as $val)
  {{$val->comment()->id}}
@endforeach

推荐阅读