2017-10-12 23 views
0

私は、次のリポジトリProductsを持っており、各製品は、私が達成しようとしています何Categoriesと多くのBiddersLaravelリポジトリwhereHas - 複数

多くを持つことができますが

$products = Products::whereHas('categories', function ($category) { 


})->whereHas('bidders', function ($bidder) { 

})->get(); 

この(リポジトリがなければ)以下れますしかし、私はリポジトリが適切に配置され、依然としてwhereHasクエリを実行できるようにしようとしていますので、私のリポジトリではメソッドを作成しました:

public function whereHas($attribute, \Closure $closure = null) 
{ 
    return $this->model->whereHas($attribute, $closure); 
} 

これはうまく動作しますが、私は複数使用している場合のに対し、私のメインのクエリでそれらのいずれかを使用していた場合にのみ:

$products = $this->products->whereHas('categories', function ($category) { 
    $category->where('id', '=', 1); 
})->whereHas('bidders', function($bidders) { 

})->get(); 

私は、次のエラーを取得しています:

Unknown column 'has_relation'

Column not found: 1054 Unknown column 'has_relation' in 'where clause' (SQL: select * from products where exists (select * from categories inner join products_categories on categories . id = products_categories . categories_id where products . id = products_categories . products_id and id = 1) and (has_relation = sections))

私が見ている問題は、最初のwhereHasのアイテムのコレクションを返すことです。つまり、2番目のアイテムを計算できません。私が間違っているところへのアイデアは?

答えて

0

あなたは期待通りに動作しますwhereHas

$products = $this->products->whereHas(['categories', function 
     ($category) { 
    $category->where('id', '=', 1); 
},'bidders' => function($bidders) { 

}])->get(); 

に複数のリレーションを持つクロージャを渡すことができます。