laravel ORM 或条件 操作数据库

1、 没有使用外部变量
代码 : 

$count_school = $Wifi
            ->where(function ($query) {
                $query->where('type', '=', 3)
                    ->orWhere('type', '=', 4);
            })
            ->where('level', 0)
            ->count();

sql语句 :

select count(*) from `wifi` where (`type` = 3 or `type` = 4) and `level` = 0


2、使用外部变量
代码 : 

$type1 = 3;
$type2 = 4;
$query = EduWifi::where(function ($query) use ($type1,$type2) {
    $query->where('type', '=', $type1)
    ->orWhere('type', '=', $type2);
    })
    ->where('level', 0)
    ->count();

sql语句 : 

select count(*) from `wifi` where (`type` = 3 or `type` = 4) and `level` = 0

猜你喜欢

转载自blog.csdn.net/weixin_40154252/article/details/106237469