laravel-wherehasin扩展使用方法
程序员文章站
2022-04-09 20:02:18
...
$data = CellRegister::with( 'members')
// ->with(['members' => function ($q) use ($param){
//
// if(isset($param['keywords'])){
// $q->orWhere('user_name', 'like', '%'.$param['keywords'].'%');
// }
//
// }])
->orWhereHas('members', function ($query) use ($param){
if(isset($param['keywords'])){
$query->where('user_name', 'like', '%'.$param['keywords'].'%');
}
})
//->where('cell_name', 'like', '%'.$param['keywords'].'%')
->with(['logs' => function ($q) use ($approve_type){
$q->where('approve_type', $approve_type);
}])
->whereRaw($where)
->select('*')->orderBy('id', 'DESC')->paginate($limit);
Laravel wherehasin
是一个可以提升Laravel ORM
关联关系查询性能的扩展包,可以替代Laravel ORM
中的whereHas
以及whereHasMorphIn
查询方法。
打印的sql如下
array:4 [
0 => array:3 [
"query" => "select count(*) as aggregate from `cell_register` where (exists (select * from `cell_member` where `cell_register`.`id` = `cell_member`.`cell_id` and `user_name` like ? and `cell_member`.`deleted_at` is null) and status < 99 or cell_name like '%门吹%' ) and `cell_register`.`deleted_at` is null"
"bindings" => array:1 [
0 => "%门吹%"
]
"time" => 12.95
]
1 => array:3 [
"query" => "select * from `cell_register` where (exists (select * from `cell_member` where `cell_register`.`id` = `cell_member`.`cell_id` and `user_name` like ? and `cell_member`.`deleted_at` is null) and status < 99 or cell_name like '%门吹%' ) and `cell_register`.`deleted_at` is null order by `id` desc limit 11 offset 0"
"bindings" => array:1 [
0 => "%门吹%"
]
"time" => 13.56
]
2 => array:3 [
"query" => "select * from `cell_member` where `cell_member`.`cell_id` in (7) and `cell_member`.`deleted_at` is null"
"bindings" => []
"time" => 12.92
]
3 => array:3 [
"query" => "select * from `cell_approval_log` where `cell_approval_log`.`grade_id` in (7) and `approve_type` = ? and `cell_approval_log`.`deleted_at` is null"
"bindings" => array:1 [
0 => 2
]
"time" => 12.14
]
]
需求:
2个表,模糊查询
有2张表,用原生sql可以轻易解决。
这里是ORM操作
上一篇: Oracle UNDO 监控