eugenes / laravel-builder
There is no license information available for the latest version (v0.20) of this package.
v0.20
2022-06-24 03:34 UTC
Requires
- php: >=7.0
Requires (Dev)
- fzaninotto/faker: ^1.4
- laravel/laravel: >=5.5
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.5|~9
This package is auto-updated.
Last update: 2024-04-24 09:53:54 UTC
README
# LARAVEL-Builder 这是一个可以提升`Laravel ORM`关联关系查询性能的扩展包,
环境
- PHP >= 7
- laravel >= 5.5
安装
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