替代laravel whereHas方法
环境
PHP >= 7
laravel >= 5.5
安装
gitee地址
gitee.com/yjshop/laravel-builder.g...
composer require eugenes/laravel-builder
简介
Laravel
的关联关系查询whereHas
在日常开发中给我们带来了极大的便利,但是在主表数据量比较多的时候会有比较严重的性能问题,主要是因为whereHas
用了where exists (select * ...)
这种方式去查询关联数据。
通过这个扩展包提供的whereHasIn,whereHasJoin
方法
方法
whereHasIn
App(CompanyStaffModel::class)->whereHasIn('section', function ($query) {
$query->where('id', 1);
})->first();
SELECT *
FROM `lk_company_staff`
WHERE `lk_company_staff`.`id` IN (SELECT `lk_company_staff_section_relation`.`userid`
FROM `lk_company_staff_section_relation`
WHERE `lk_company_staff`.`id` = `lk_company_staff_section_relation`.`userid`
AND `id` = 1) LIMIT 1
whereHasJoin
App(CompanyStaffModel::class)->yjselect('*')->whereHasJoin('section', function ($query) {
$query->yjwhere('id', 1);
})->first();
select `lk_company_staff`.*
from `lk_company_staff`
left join `lk_company_staff_section_relation`
on `lk_company_staff_section_relation`.`userid` = `lk_company_staff`.`id`
where ((`lk_company_staff_section_relation`.`id` = 1)) limit 1
yjwhere
App(CompanyStaffModel::class)->yjwhere('id', 1)->first();
select *
from `lk_company_staff`
where `lk_company_staff`.`id` = 1 limit 1
yjselect
App(CompanyStaffModel::class)->yjselect('id')->first();
select `lk_company_staff`.`id`
from `lk_company_staff` limit 1
yjsum
yjpluck
yjorderBy
yjorderByDesc
orWhereHasIn
orWhereHasNotIn
whereHasMorphIn
orWhereHasMorphIn
License
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: