PHPackages                             dcat/laravel-wherehasin - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. dcat/laravel-wherehasin

ActiveLibrary[Database &amp; ORM](/categories/database)

dcat/laravel-wherehasin
=======================

Laravel ORM whereHasIn

0.8.0(4y ago)247149.8k↑53.5%19[7 issues](https://github.com/jqhph/laravel-wherehasin/issues)3MITPHPPHP &gt;=7.0CI failing

Since Jun 28Pushed 4y ago3 watchersCompare

[ Source](https://github.com/jqhph/laravel-wherehasin)[ Packagist](https://packagist.org/packages/dcat/laravel-wherehasin)[ Docs](https://github.com/jqhph/dcat-admin)[ RSS](/packages/dcat-laravel-wherehasin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (8)Used By (3)

LARAVEL WHERE HAS IN
====================

[](#laravel-where-has-in)

 [![](https://camo.githubusercontent.com/ffe7adf17b1d38c79e0e81f80884e0e21f43129b2b96c7a123ea3bd032f09cb2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d3733383944382e7376673f7374796c653d666c6174)](https://github.com/jqhph/laravel-wherehasin/blob/master/LICENSE) [ ![Build Status](https://github.com/jqhph/laravel-wherehasin/workflows/Phpunit/badge.svg) ](https://github.com/jqhph/laravel-wherehasin/actions) [ ![StyleCI](https://camo.githubusercontent.com/a0f468291b1a8883f44d51930cc9ef455d37fd0fcbebb8e65acf3e36277ba9db/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3231353733383739372f736869656c64) ](https://styleci.io/repos/215738797) [![](https://camo.githubusercontent.com/d36a54377e40f91315462f31f0c4f3510216fb7fb0aa7fef85ad34072d771ffb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6a716870682f6c61726176656c2d7768657265686173696e2e7376673f636f6c6f723d343039394445)](https://github.com/jqhph/laravel-wherehasin/releases) [![](https://camo.githubusercontent.com/9978e6d114a610a1eb1d93579821e8205ca8fdca755b9ad64aed7506efbcfffb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646361742f6c61726176656c2d7768657265686173696e2e7376673f636f6c6f723d)](https://packagist.org/packages/dcat/laravel-wherehasin) [![](https://camo.githubusercontent.com/58ee6668ad0f0c53e9a258560f4eb7483b281fe1b8520bf8bdc2cb248bff4a46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372b2d3539613966382e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/58ee6668ad0f0c53e9a258560f4eb7483b281fe1b8520bf8bdc2cb248bff4a46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372b2d3539613966382e7376673f7374796c653d666c6174)

`Laravel wherehasin`是一个可以提升`Laravel ORM`关联关系查询性能的扩展包，可以替代`Laravel ORM`中的`whereHas`以及`whereHasMorphIn`查询方法。

环境
--

[](#环境)

- PHP &gt;= 7
- laravel &gt;= 5.5

安装
--

[](#安装)

```
composer require dcat/laravel-wherehasin
```

### 简介

[](#简介)

`Laravel`的关联关系查询`whereHas`在日常开发中给我们带来了极大的便利，但是在**主表**数据量比较多的时候会有比较严重的性能问题，主要是因为`whereHas`用了`where exists (select * ...)`这种方式去查询关联数据。

通过这个扩展包提供的`whereHasIn`方法，可以把语句转化为`where id in (select xxx.id ...)`的形式，从而提高查询性能，下面我们来做一个简单的对比：

> 当主表数据量较多的情况下，`where id in`会有明显的性能提升；当主表数据量较少的时候，两者性能相差无几。

主表`test_users`写入`130002`条数据，关联表`test_user_profiles`写入`1002`条数据，查询代码如下

```
