Laravel larvel查询((Key1=A和Key2=B)或(Key1=B和Key2=A))和Key3=C
我按照以下方式为基本查询构建查询Laravel larvel查询((Key1=A和Key2=B)或(Key1=B和Key2=A))和Key3=C,laravel,eloquent,Laravel,Eloquent,我按照以下方式为基本查询构建查询 // e.g. $params = ['id' => 'a', 'name' = 'ken'] foreach ($params as $key => $val) { $query->where($key, "=", $val); } 但是,我找不到一种方法来进行下面的复杂查询。如何构建查询 ((Key1=A and Key2=B) or (Key1=B and Key2=A)) and Key3=C 如果查询是(A或B)和(C或D)
// e.g. $params = ['id' => 'a', 'name' = 'ken']
foreach ($params as $key => $val) {
$query->where($key, "=", $val);
}
但是,我找不到一种方法来进行下面的复杂查询。如何构建查询
((Key1=A and Key2=B) or (Key1=B and Key2=A)) and Key3=C
如果查询是(A或B)和(C或D)
如果需要((x和y)或(z和v))和z
Model::where(function ($q) use ($a, $b) {
$q->where(function($q) use($a, $b){
$q->where('key1', $a)->where('key2', $b);
})
->orWhere(function($q) use($a, $b){
$q->where('key1', $b)->where('key2', $a);
})
})
->where('key3', $c)
->get();
很抱歉我没有及时回复。我不知怎的犯了语法错误。我正在努力修复它。
Model::where(function ($q) use ($a, $b) {
$q->where(function($q) use($a, $b){
$q->where('key1', $a)->where('key2', $b);
})
->orWhere(function($q) use($a, $b){
$q->where('key1', $b)->where('key2', $a);
})
})
->where('key3', $c)
->get();