2016-10-18 19 views
-1

私は、次の表があります。Laravel複雑なクエリ

料理

id 
name 

お客様

id 
name 

成分

Dishes_Ingredients(関係皿に入れて、テーブルや食材)

Customers_Allergic_Ingredients

id 
dish_id 
ingredient_id 
は(顧客がに耐えられ

id 
customer_id 
ingredient_id 

Customers_Intolerance_Ingredients(顧客が特定の成分にアレルギーがあります)特定の成分)

id 
customer_id 
ingredient_id 

私は、データベースから以下の情報を入手する必要があります。与えられたcustomer_idのために、私は顧客がLaravelクエリビルダを使用して、にアレルギーと不寛容にないないあるすべての料理を取得したいです。

これは私がこれまで試したものです:

$dishes = DB::table('dishes') 
      ->join('dishes_ingredients', 'dishes.id', '=', 'dishes_ingredients.dish_id') 
      ->join('customers_allergic_ingredients', 'dishes_ingredients.ingredient_id', '<>', 'customers_allergic_ingredients.ingredient_id') 
      ->join('customers_intolerance_ingredients', 'dishes_ingredients.ingredient_id', '<>', 'customers_intolerance_ingredients.ingredient_id') 
      ->where('customers.id', 1) 
      ->select('dishes.id', 'dish_translations.name') 
      ->get(); 
+0

にアレルギーではない食材を使用したすべての料理は、あなたがこれまでにしようとしているものを私たちに示してください、あなたを与えるだろう。 –

+1

StackOverflowは、少しでも試していない場合、複雑なソリューションを得る場所ではありません。最初にいくつかのトピック(SQL、PHP、Laravel)を学習してみてください...それがうまくいかない場合は、もう一度やり直してください...そしてその後、ここに来て質問をしてください... –

+0

Plsはあなたの試みたコードを編集します。その後、私たちはあなたを助けることができるでしょう。 –

答えて

0

私はLaravelを知らないが、あなたは自分の問題を解決したい場合:関係:

SELECT * 
FROM Dishes d 
WHERE d.id NOT IN 
    (
     SELECT DISTINCT dish_id 
     FROM Dishes_Ingridients 
     WHERE ingredient_id IN 
      (SELECT DISTINCT ingredient_id FROM Customers_Allergic_Ingredients cai WHERE cai.customer_id = ?) 
      OR 
      ingredient_id IN 
      (SELECT DISTINCT ingredient_id FROM Customers_Intolerance_Ingredients cii WHERE cii.customer_id = ?) 
    ) 
+0

Customers_Allergic_Ingredientsは何dish_idを持っていない... – user335672

+0

私はこのクエリを試してみました: 'SELECT * \t料理 \t INNER FROM dishes.id = dishes_ingredients.dish_id \t WHERE dishes_ingredients ON dishes_ingredients \tを登録しよう。 \tと \t \t \tにないdishes_ingredients.ingredient_id(CAI WHERE cai.customer_id = 1 customers_allergic_ingredients FROM cai.ingredient_idを選択) '(1 customers_intolerance_ingredients FROM CII WHERE cii.customer_id =をcii.ingredient_idを選択) \t \t \tではありませんingredient_id – user335672

+0

しかし、それは動作しません、それは私にすべての料理を返します... – user335672

0
$dishes = DB::table("dishes AS a") 
        ->select(array("a.*")) 
        ->join("dishes_ingredients AS b", "a.id", "b.dish_id") 
        ->join("Customers_Allergic_Ingredients AS c", "b.id", "=", "c.ingredient_id") 
        ->where("c.customer_id", "!=", $customer_id) 
        ->groupBy("a.id") 
        ->get(); 

これは、顧客が