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

Php 无法显示Laravel刀片中的相关变量,php,laravel,orm,eloquent,blade,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>

我在显示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
使用ORM而不是DB


使用ORM而不是DB。

它适用于第一项,因为您使用ORM(“好”方法),而使用DB::table时,您得到的不是对象,而是数组。所以请使用@Alex solution。它适用于第一项,因为您使用ORM(“好”方法),而当使用DB::table时,您得到的不是对象,而是数组。所以使用@Alex解决方案。
public function Count()
{
   $news=News::find(1);
  echo $news->comments()->count();
 }
public function GetAll()
{
$news = News::with('comments')->get();
 return View('index',['news'=>$news]);
}