簡體   English   中英

Laravel存儲庫,其中有多個

[英]Laravel Repositories whereHas — multiple

我有以下存儲庫Products ,每個產品可以有多個Categories和多個Bidders

我要實現的目標如下(沒有存儲庫)

$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'

列未找到:從選擇*:在'where子句'(SQL 1054未知列'has_relation' products ,其中存在(SELECT * FROM categories內加入products_categoriescategoriesid = products_categoriescategories_id其中productsid = products_categoriesproducts_idid = 1)和( has_relation =部分))

我看到的問題是,它在第​​一個whereHas上返回項目集合,這意味着它無法計算第二個。 對我要去哪里錯有任何想法嗎?

您可以將具有多個關系的閉包傳遞給whereHas

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

}])->get();

它將按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM