PHPackages                             dscmall/laravel-orm-hasin - 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. dscmall/laravel-orm-hasin

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

dscmall/laravel-orm-hasin
=========================

Laravel framework relation has in implement

v1.2.0(5y ago)02.2kMITPHPPHP &gt;=7.1.3

Since Dec 21Pushed 5y agoCompare

[ Source](https://github.com/dscmall/laravel-orm-hasin)[ Packagist](https://packagist.org/packages/dscmall/laravel-orm-hasin)[ RSS](/packages/dscmall-laravel-orm-hasin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (10)Used By (0)

LARAVEL HASIN
=============

[](#laravel-hasin)

`hasin`是一个基于`where in`语法实现的`Laravel ORM`关联关系查询的扩展包，部分业务场景下可以替代`Laravel ORM`中基于`where exists`语法实现的`has`，以获取更高的性能。

环境
--

[](#环境)

- PHP &gt;= 7.1
- laravel &gt;= 5.8

安装
--

[](#安装)

```
composer require dscmall/laravel-orm-hasin
```

简介
--

[](#简介)

`Laravel ORM`的关联关系非常强大，基于关联关系的查询`has`也给我们提供了诸多灵活的调用方式，然而某些情形下，`has`使用了**where exists**语法实现

#### `select * from A where exists (select * from B where A.id=B.a_id)`

[](#select--from-a-where-exists-select--from-b-where-aidba_id)

> exists是对外表做loop循环，每次loop循环再对内表（子查询）进行查询，那么因为对内表的查询使用的索引（内表效率高，故可用大表），而外表有多大都需要遍历，不可避免（尽量用小表），故内表大的使用exists，可加快效率。

但是当**A表**数据量较大的时候，就会出现性能问题，那么这时候用**where in**语法将会极大的提高性能

#### `select * from A where A.id in (select B.a_id from B)`

[](#select--from-a-where-aid-in-select-ba_id-from-b)

> in是把外表和内表做hash连接，先查询内表，再把内表结果与外表匹配，对外表使用索引（外表效率高，可用大表），而内表多大都需要查询，不可避免，故外表大的使用in，可加快效率。

因此在代码中使用`has(hasMorph)`或者`hasIn(hasMorphIn)`应由**数据体量**来决定……

```
