如何在laravel 5中括起括号内的查询条件

时间:2016-01-19 07:10:51

标签: php laravel-5 laravel-query-builder

在laravel 5中,我使用下面的查询构建器

$user_social = DB::table('users')
        ->where('email','=',$credentials['email'])
        ->where('facebook', '!=', '')
        ->orWhere('google', '!=', '')
        ->get(['facebook','google']);

我正在收到由查询构建器生成的查询

"select * from `users` where `email` = ? and `facebook` != ? or `google` != ?"

进行此类查询需要进行哪些更改。

"select * from `users` where `email` = ? and (`facebook` != ? or `google` != ?")

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我会尝试类似this的内容:

$user_social = DB::table('users')
        ->where('email', '=', $credentials['email'])
        ->orWhere(function($query)
        {
            $query->where('facebook', '!=', '')
                  ->where('google', '!=', '');
        })
        ->get(['facebook','google']);

这会生成一个像这样的查询

select * from users where email = 'email' or (facebook != '' and google != '')