2016-09-26 4 views
1
でクエリ

こんにちはみんな、whereHas Laravel

 $filterArray = explode("_", $filters); 

     $data['articles'] = \DB::table('products')->join('product_category', function ($q) { 
      $q->on('product_category.product_id', '=', 'products.id'); 
     })->where('product_category.category_id', '=', $id) 
      ->select('products.*') 
      ->whereBetween('price_retail_1', array($priceFrom, $priceTo)) 
      ->whereHas('filters', function ($query, $filterArray) { 
       $query->whereIn('filter_id', $filterArray); 
      }) 
      ->orderBy('products.' . $sort, $sortOrder) 
      ->get(); 
    } 

私は次のクエリを持っていると私はwhereHas法上のいくつかの問題を抱えています。 $ filterArray変数は、私は推測していますどのような関数(または少なくともの範囲外であるので、私はエラーに

Unknown column 'has' in 'where clause 

最も可能性を取得しています。この問題を解決する方法上の任意のヘルプは高く評価されています。

あなたは、クエリビルダのコンテキストで whereHas方法を使用することはできません。 whereHas方法が唯一の雄弁モデルとその関係から来ている 雄弁クエリビルダのためである。

あなたは何ができるか

答えて

1

joinsを使用することです。だから、あなたが試すことができます このように:

->join('filters', 'products.filter_id', '=', 'filters.filter_id') 
+0

マインドは私の例を与える:

$filterArray = explode("_", $filters); $data['articles'] = \DB::table('products')->join('product_category', function ($q) { $q->on('product_category.product_id', '=', 'products.id'); })->where('product_category.category_id', '=', $id) ->select('products.*') ->whereBetween('price_retail_1', array($priceFrom, $priceTo)) ->join('filters', 'products.filter_id', '=', 'filters.filter_id') ->whereIn('filter_id', $filterArray); ->orderBy('products.' . $sort, $sortOrder) ->get(); 

私はあなたがそう、ここでこれらの2つのテーブルを接続するだけでデータ例である方法がわかりませんか?フィルタテーブルには同じ製品の複数の行が含まれる可能性があるため、フィルタ配列で指定されたデータに基づいてOR WHERE検索を実行する必要があります。 – kjanko

+0

私はエントリーを再編集する。 –

関連する問題