我有这个问题:
SELECT * FROM users
WHERE
gender = "$gender" AND country = "$country"
AND ((city = "$city" AND status = "1")
OR (status IN(2,3)))";
如何在Lumen中编写上述查询?
到目前为止我尝试的是:
$users = User::where('country', '=', $country)
->where('gender', '=', $gender)
->where(function ($users) {
$users->where('city', '=', '$city')
->where('status', '=', "1");
})
->whereIn('status', [2, 3])
->first();
但是这个查询并没有返回预期的结果。
知道我的查询中有什么错误吗?
答案 0 :(得分:1)
尝试以下代码。
$users = User::where('country', '=', $country)
->where('gender', '=', $gender)
->where(function ($query) use ($city) {
$query->where(function($query) use ($city) {
$query->where('city', '=', '$city')
->where('status', '=', "1");
})->orWhereIn('status', [2, 3]);
})->first();