2016-10-13 7 views
1

ですから、私はこの質問に少し突っ込んでいます。Laravel Query((or and or)or)と

私の問題を説明しよう。 私のモデルでは、 $ a、$ b、$ cというモデルで次のように表示されます。

ただし、$ bと$ cはnullでもかまいません。

だから私のクエリは、((from_user_id = $ Aまたはto_user_id = $ A)pushnotificationsから*選択して(from_user_id = $ bまたはto_user_id = $ b)または(to_user_id = $ Aこの のようなものである必要があり、 from_user_id = 0))とid = $ C

答えて

2

使用はwhere()orWhere()ネストされた:あなたの答えのための

Model::where(function($q) { 
    $q->where('from_user_id', $a) 
     ->orWhere('to_user_id', $a) 
})->where(function($q) { 
    $q->where('from_user_id', $b) 
     ->orWhere('to_user_id', $b) 
})->orWhere(function($q) { 
    $q->where('from_user_id', $a) 
     ->orWhere('to_user_id', $0) 
})->where('id', $c) 
->get(); 
+0

Thxをしますが、いくつかの問題が依然として存在しています。まず、$ bはnullである可能性があります。この場合、無効な結果が返されます。最後のどこで( 'id'、$ c)、フィルタリングされていないはずです。 –

+0

(from_user_id' =?または 'to_user_id' =?)、(' from_user_id' =?または 'to_user_id' =?)または(' to_user_id' =?)のどこから 'select * from' pushnotifications ' ?と 'from_user_id' =?)と' id' <? ' –

+0

親スコープから変数を継承するのを忘れないでください! – Neat