无法在Laravel刀片中显示相关变量

时间:2016-06-02 09:08:38

标签: php laravel orm eloquent blade

我在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();
 }

1 个答案:

答案 0 :(得分:2)

public function GetAll()
{
$news = News::with('comments')->get();
 return View('index',['news'=>$news]);
}

使用ORM代替DB。