这篇文章主要为大家展示了“laravel中orWhere怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“laravel中orWhere怎么用”这篇文章吧。
laravel框架下 where 条件查询很简单,这里就不写了,这里主要记录一下平常不怎么用的 orWhere 多条件 or 查询语句的拼接,示例如下:
(1)SQL 语句:
select `id` from `user` where (`email` = 'admin@phpernote.com' and `password` = '123456') or (`mobile` = '13681127231' and `password` = '123456');
laravel model 语句:
User::select(['id']) ->where(function ($query) use ($userName, $password) { $query->where('password', '=', '123456')->where('email', '=', 'admin@phpernote.com'); }) ->orWhere(function ($query) use ($userName, $password) { $query->where('password', '=', '123456')->where('mobile', '=', '13681127231'); }) ->first();
(2)SQL 语句:
select count(*) from `user` where `type` = 1 and `valid_type` = 2 and (`valid_end` < 1560738570 or `valid_begin` > 1560738570);
laravel model 语句:
$model = User::where('type', 1)->where('valid_type', '=', 2)->where(function ($query) { $query->where('valid_end', '<', 1560738570)->orWhere(function ($query) { $query->where('valid_begin', '>', 1560738570); });})->first();
(3)SQL 语句:
select count(*) from `user` where `valid_type` = 2 or (`valid_type` = 3 and (`valid_end` < 1560738570 or `valid_begin` > 1560738570));
laravel model 语句:
$model = User::where(function ($query) { $query->where('valid_type', 2);})->orWhere(function ($query) { $query->where('valid_type', '=', 3)->where(function ($query) { $query->where('valid_end', '<', 1560738570)->orWhere(function ($query) { $query->where('valid_begin', '>', 1560738570); }); });})->first();
以上是“laravel中orWhere怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!