【发布时间】:2016-10-01 22:13:45
【问题描述】:
我无法在 laravel Blade 中显示相关变量
public function GetAll()
{
$news=DB::table('news')->get();
return View('index',['news'=>$news]);
}
在视图中:
@foreach($news as $new)
...
<a href="#">{{$new->comments()->count()}} Comments</a>
...
@endforeach
它也不适用于对象的任何变量但对第一项效果很好:
public function Count()
{
$news=News::find(1);
echo $news->comments()->count();
}
【问题讨论】:
-
它适用于第一项,因为您使用 ORM(“好”方式),而使用 DB::table 时,您获取的不是对象而是数组。所以使用@Alex 解决方案。
标签: php laravel orm eloquent blade