2016-10-27 9 views
0

私はの商品を持っています。です。 品目 hasMany templateItemsこの関係では、items.idを外部キーとして使用します。template_items条件付き結合はどのように行うのですか? Laravel 5

私はすべての項目を返すだけtemplate_items.templateが$ IDに等しい関連templateItemsを含めるしようとしています。

私はこれを試しました。

 $items = Item::with('templateItems', function($query) use ($id) { 
     $query->where('template_items.template', $id); 
     })->get(); 

私が取得:Builder.phpライン1150で

ErrorException: 爆発は()オブジェクトが、私は何をしないのです

を与えたり、より良い方法はあり、パラメータ2が文字列であることを期待?おかげさまで

答えて

0

あなたのクエリは次のようにする必要があります:

$items = Item::with(['templateItems' => function($query) use ($id) { 
    $query->where('template_items.template', $id); 
    }])->get(); 
+0

それをやったこと。ありがとう! – RushVan