如何处理和在哪里和或在鲁门的条件?

时间:2015-09-23 04:00:14

标签: php mysql laravel laravel-5 lumen

我有这个问题:

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();

但是这个查询并没有返回预期的结果。

知道我的查询中有什么错误吗?

1 个答案:

答案 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();