我在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();
}
答案 0 :(得分:2)
public function GetAll()
{
$news = News::with('comments')->get();
return View('index',['news'=>$news]);
}
使用ORM代替DB。