简体   繁体   English

Where子句中的流明和OR条件

[英]Lumen AND OR conditions in Where clause

I am trying to run following query in Lumen Framework: 我试图在Lumen框架中运行以下查询:

SELECT * FROM user WHERE (username = $username OR email = $username) AND password = $password AND flag = 1;

My Lumen code: 我的流明代码:

$login =  User::where('pass', '=', md5($pass))
               ->where('flag', '=', $flag)
               ->where('username', '=', $username)
               ->orWhere('email', '=', $username)->first();

Somehow this code always return true and bypass the login. 不知何故,此代码始终返回true并绕过登录。 What is wrong in that query? 那个查询有什么问题?

When I remove orWhere from query it works perfect for username. 当我从查询中删除orWhere ,它非常适合用户名。

If you want to group the two conditions (username and email) into a single condition (surrounded by parenthesis), you will have to do it like this: 如果要将两个条件(用户名和电子邮件)分组到一个条件(用括号括起),则必须这样做:

User::where('pass', '=', md5($pass))
    ->where('flag', '=', $flag)
    ->where(function ($query) use ($username) {
        $query->where('username', '=', $username)
              ->orWhere('email', '=', $username);
    })->first();

Here is the documentation on advanced where conditions for the Laravel query builder (which Lumen uses). 以下是Laravel查询构建器(Lumen使用的)条件的高级文档。

It seems to me that what you want is this: 在我看来,你想要的是这个:

User::where('pass', '=', md5($pass))
    ->where('flag', '=', $flag)
    ->where(function ($query) use ($username) {
        $query->where('username', '=', $username)
              ->orWhere('email', '=', $username);
    })->first();

Also please don't use md5 , try re hashing all of your passwords with bcrypt() 另请不要使用md5 ,尝试使用bcrypt()重新散列所有密码

Follow laravel advance where http://laravel.com/docs/4.2/queries#advanced-wheres 请关注laravel advance http://laravel.com/docs/4.2/queries#advanced-wheres

User::where('pass', '=', md5($pass))
->where('flag', '=', $flag)
->where(function ($query) use ($username) {
    $query->where('username', '=', $username)
          ->where('email', '=', $username);
})->first();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM