Eloquent Laravel 多个 like where 和 where 子句
Eloquent Laravel multiple like where and where clause
我有以下 Mysql 有效的查询。
mysql:
select * from operatories op
left join locations al on op.location = al.location
where op.opname like '%NP%' or op.opname like '%OPEN%'
and al.isOfficeClosed = 0;
这行得通,我得到了我期望的数据,但我试图在 Laravel 查询生成器中编写它,但它没有获取最后一个 and
al.isOfficeClose = 0。有什么你们可以发现我做错了什么吗?
Laravel:
$locs = DB::table('operatories as op')
->leftJoin(DB::raw('locations al'), function($join) {
$join->on('op.Location', '=', 'al.location');
})
->Where('op.opname','LIKE','%NP%')
->orWhere('op.opname','LIKE','%OPEN%')
->where('al.isOfficeClosed', '=','0');
您可以为 where
子句定义一个函数,如下所示:
$locs = DB::table('operatories as op')
->leftJoin(DB::raw('locations al'), function($join) {
$join->on('op.Location', '=', 'al.location');
})
->where(function($q) {
$q->where('op.opname','LIKE','%NP%');
$q->orWhere('op.opname','LIKE','%OPEN%');
})
->where('al.isOfficeClosed', '=','0');
顺便说一句,
select * from operatories op
left join locations al on op.location = al.location
where op.opname like '%NP%' or op.opname like '%OPEN%'
and al.isOfficeClosed = 0;
不同于(注意括号)
select * from operatories op
left join locations al on op.location = al.location
where (op.opname like '%NP%' or op.opname like '%OPEN%')
and al.isOfficeClosed = 0;
我有以下 Mysql 有效的查询。
mysql:
select * from operatories op
left join locations al on op.location = al.location
where op.opname like '%NP%' or op.opname like '%OPEN%'
and al.isOfficeClosed = 0;
这行得通,我得到了我期望的数据,但我试图在 Laravel 查询生成器中编写它,但它没有获取最后一个 and
al.isOfficeClose = 0。有什么你们可以发现我做错了什么吗?
Laravel:
$locs = DB::table('operatories as op')
->leftJoin(DB::raw('locations al'), function($join) {
$join->on('op.Location', '=', 'al.location');
})
->Where('op.opname','LIKE','%NP%')
->orWhere('op.opname','LIKE','%OPEN%')
->where('al.isOfficeClosed', '=','0');
您可以为 where
子句定义一个函数,如下所示:
$locs = DB::table('operatories as op')
->leftJoin(DB::raw('locations al'), function($join) {
$join->on('op.Location', '=', 'al.location');
})
->where(function($q) {
$q->where('op.opname','LIKE','%NP%');
$q->orWhere('op.opname','LIKE','%OPEN%');
})
->where('al.isOfficeClosed', '=','0');
顺便说一句,
select * from operatories op
left join locations al on op.location = al.location
where op.opname like '%NP%' or op.opname like '%OPEN%'
and al.isOfficeClosed = 0;
不同于(注意括号)
select * from operatories op
left join locations al on op.location = al.location
where (op.opname like '%NP%' or op.opname like '%OPEN%')
and al.isOfficeClosed = 0;