承接 biiiiiigmonster/hasin 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

biiiiiigmonster/hasin

Composer 安装命令:

composer require biiiiiigmonster/hasin

包简介

Laravel framework relation has in implement

README 文档

README

English | 中文

LARAVEL HASIN

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Coverage Status Scrutinizer Code Quality Total Downloads

The hasin is composer package based on where in syntax to query the relationship of laravel ORM, which can replace has based on where exists syntax in some business scenarios to obtain higher performance.

Installation

Laravel Version Install command
Laravel 12 composer require biiiiiigmonster/hasin:^5.0
Laravel 11 composer require biiiiiigmonster/hasin:^4.0
Laravel 10 composer require biiiiiigmonster/hasin:^3.0
Laravel 9 composer require biiiiiigmonster/hasin:^2.0
Laravel 5.5 ~ 8 composer require biiiiiigmonster/hasin:^1.0

Introductions

The relationship of laravel ORM is very powerful, and the query has based on the relationship also provides us with many flexible calling methods. However, in some cases, has is implemented with where exists syntax.

For example:

// User hasMany Post
User::has('posts')->get();

select * from users where exists (select * from posts where users.id = posts.user_id)

'exists' is a loop to the external table, and then queries the internal table (subQuery) every time. Because the index used for the query of the internal table (the internal table is efficient, so it can be used as a large table), and how much of the external table needs to be traversed, it is inevitable (try to use a small table), so the use of exists for the large internal table can speed up the efficiency.

When the User table has a large amount of data, there will be performance problems, so the where in syntax will greatly improve the performance.

select * from users where users.id in (select posts.user_id from posts)

'in' is to hash connect the appearance and inner table, first query the inner table, then match the result of the inner table with the appearance, and use the index for the outer table (the appearance is efficient, and large tables can be used). Most of the inner tables need to be queried, which is inevitable. Therefore, using 'in' with large appearance can speed up the efficiency.

Therefore, the use of has(hasMorph) or hasIn(hasMorphIn) in code should be determined by data size

/**
 * SQL:
 * 
 * select * from `users` 
 * where exists 
 *   ( 
 *      select * from `posts` 
 *      where `users`.`id` = `posts`.`user_id` 
 *   ) 
 * limit 10 offset 0
 */
$users = User::has('posts')->paginate(10);

/**
 * SQL:
 * 
 * select * from `users` 
 * where `users`.`id` in  
 *   ( 
 *      select `posts`.`user_id` from `posts` 
 *   ) 
 * limit 10 offset 0
 */
$users = User::hasIn('posts')->paginate(10);

Usage example

hasIn(hasMorphIn) supports all Relations in laravel ORM. The call mode and internal implementation are completely consistent with has(hasMorph) of the framework.

hasIn

// hasIn
User::hasIn('posts')->get();

// orHasIn
User::where('age', '>', 18)->orHasIn('posts')->get();

// doesntHaveIn
User::doesntHaveIn('posts')->get();

// orDoesntHaveIn
User::where('age', '>', 18)->orDoesntHaveIn('posts')->get();

whereHasIn

// whereHasIn
User::whereHasIn('posts', function ($query) {
    $query->where('votes', '>', 10);
})->get();

// orWhereHasIn
User::where('age', '>', 18)->orWhereHasIn('posts', function ($query) {
    $query->where('votes', '>', 10);
})->get();

// whereDoesntHaveIn
User::whereDoesntHaveIn('posts', function ($query) {
    $query->where('votes', '>', 10);
})->get();

// orWhereDoesntHaveIn
User::where('age', '>', 18)->orWhereDoesntHaveIn('posts', function ($query) {
    $query->where('votes', '>', 10);
})->get();

hasMorphIn

Image::hasMorphIn('imageable', [Post::class, Comment::class])->get();

Nested Relation

User::hasIn('posts.comments')->get();

Testing

composer test

Tips: before testing, you need to configure your database connection in the phpunit.xml.dist.

License

MIT

biiiiiigmonster/hasin 适用场景与选型建议

biiiiiigmonster/hasin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 584.71k 次下载、GitHub Stars 达 153, 最近一次更新时间为 2020 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「orm」 「laravel」 「relation」 「whereHas」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 biiiiiigmonster/hasin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 biiiiiigmonster/hasin 我们能提供哪些服务?
定制开发 / 二次开发

基于 biiiiiigmonster/hasin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 584.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 154
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 153
  • Watchers: 3
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-21