替代laravel whereHas方法

LARAVEL-Builder

这是一个可以提升Laravel ORM关联关系查询性能的扩展包,

环境

  • 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

The MIT License (MIT).

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 1

Packagist 提示 There is no license information available for the latest version (v0.20) of this package.

11个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!