Laravel5.3-local or condition and global or condition under database operations (local and global orWhere), laravel5.3-orwhere

Source: Internet
Author: User

Laravel5.3-local or condition and global or condition under database operations (local and global orWhere), laravel5.3-orwhere

When the user name is not blank

SELECT * FROM 'account _ RECHARGE 'left JOIN 'order' ON 'account _ RECHARGE '. 'orderno' = 'order '. 'orderno' left join 'user _ account' ON 'order '. 'userid' = 'user _ account '. 'userid'WHERE('User _ account '. 'username' LIKE '% 18%' OR ('user _ account '. 'Your eno' LIKE '% 100') OR ('user _ account '. 'email 'LIKE' % 18% '))AND 'account _ RECHARGE '. 'ordertime' BETWEEN '2017-08-11 23:59:59' AND '2017-08-11 'ORDER'Account _ RECHARGE '. 'ordertime' DESC;

The preceding SQL statement is written in laravel5.3:

$ Model = DB: connection ('order _ user')-> table ('account _ RECHARGE ')

-> LeftJoin ('order', 'account _ RECHARGE. orderno', '=', 'order. orderno ')

-> LeftJoin ('user _ account', 'order. userid', '=', 'user _ ACCOUNT. userid ')
                 
-> Select ('account _ RECHARGE. * ', 'user _ ACCOUNT. username', 'user _ ACCOUNT. login eno', 'user _ ACCOUNT. EMail ')

-> OrderBy ('account _ RECHARGE. ordertime', 'desc ');
If (! Empty ($ username )){
$ Username = trim ($ username );
$ Model = $ model-> where (function ($ query) use ($ username ){
$ Query-> where ('user _ ACCOUNT. username', 'like', "% $ UserName % ")
-> OrWhere (function ($ query) use ($ username ){
$ Query-> where ('user _ ACCOUNT. inclueno', 'like', "% $ username % ");
});
-> OrWhere (function ($ query) use ($ username ){
$ Query-> where ('user _ ACCOUNT. EMail ', 'like', "% $ username % ");
});
});
}

Note: In the judgment that username is not empty, if use is not used, the undefined variable username will be reported.

Comparison

First

$ Model = $ model-> where (function ($ query) use ($ username ){
$ Query-> where ('user _ ACCOUNT. username', 'like', "% $ UserName % ")
-> OrWhere (function ($ query) use ($ username ){
$ Query-> where ('user _ ACCOUNT. inclueno', 'like', "% $ username % ");
});
-> OrWhere (function ($ query) use ($ username ){
$ Query-> where ('user _ ACCOUNT. EMail ', 'like', "% $ username % ");
});
});
}

And

Second

$ Model = $ model> where ('user _ ACCOUNT. userName ', 'like', "% $ username %")-> orWhere ('regioneno', 'like', "% $ username %")-> orWhere
('Email ', 'like', "% $ username % ");
}

Difference

The first is the local or condition, which only determines the or of username, and the relationship with other conditions is the same as that of the first SQL statement that appears.

The second is the global or condition. If you replace the second statement with the first statement, it is equivalent to the SQL statement.

SELECT * FROM 'account _ RECHARGE 'left JOIN 'order' ON 'account _ RECHARGE '. 'orderno' = 'order '. 'orderno' left join 'user _ account' ON 'order '. 'userid' = 'user _ account '. 'userid'WHERE'User _ account '. 'username' LIKE '% 18%' OR ('user _ account '. 'Your eno' LIKE '% 100') OR ('user _ account '. 'email 'LIKE' % 100 ')AND 'account _ RECHARGE '. 'ordertime' BETWEEN '2017-08-11 23:59:59' AND '2017-08-11 'ORDER'Account _ RECHARGE '. 'ordertime' DESC;

At this time, you will find that as long as the username exists, your other conditions will become invalid.

This is the pitfall I skipped ......

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.