首页  >  文章  >  php框架  >  laravel orWhere怎么用?(附代码示例)

laravel orWhere怎么用?(附代码示例)

藏色散人
藏色散人转载
2022-01-04 14:42:055012浏览

下面由Laravel教程栏目给大家讲解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(&#39;type&#39;, 1)->where(&#39;valid_type&#39;, &#39;=&#39;, 2)->where(function ($query) {
    $query->where(&#39;valid_end&#39;, &#39;<&#39;, 1560738570)->orWhere(function ($query) {
        $query->where(&#39;valid_begin&#39;, &#39;>&#39;, 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(&#39;valid_type&#39;, 2);
})->orWhere(function ($query) {
    $query->where(&#39;valid_type&#39;, &#39;=&#39;, 3)->where(function ($query) {
        $query->where(&#39;valid_end&#39;, &#39;<&#39;, 1560738570)->orWhere(function ($query) {
            $query->where(&#39;valid_begin&#39;, &#39;>&#39;, 1560738570);
        });
    });
})->first();

相关推荐:最新的五个Laravel视频教程

以上是laravel orWhere怎么用?(附代码示例)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:phpernote.com。如有侵权,请联系admin@php.cn删除