2016-06-02 2 views
0

私はlaravelブレードに関連する変数を示すに問題があるブレードカントショー関連の変数は

ビューで
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 

そのオブジェクトが、良い作業のいずれかの変数についてもdoesntの仕事最初の項目の場合

public function Count() 
{ 
    $news=News::find(1); 
    echo $news->comments()->count(); 
} 
+0

ORM(「良い」方法)を使用しているため、DB :: tableを使用しているときにオブジェクトを取得するのではなく、配列を使用しているため、最初の項目にはうまく機能します。 @Alexソリューションを使用してください。 – Amarnasan

答えて

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

DBの代わりにORMを使用します。

関連する問題