ahfa92/laravel-relation-joins
Composer 安装命令:
composer require ahfa92/laravel-relation-joins
包简介
Adds the ability to join on a relationship by name.
README 文档
README
This package adds the ability to join on a relationship by name.
Introduction
Eloquent doesn't offer any tools for joining, so we've been stuck with the base query builder joins. While Eloquent does have the "has" concept for existence, there are still times where you want to return information about the related entities, or aggregate information together.
I've seen other packages out there that try to accompish a goal similar to this one. I tried to get on board with at least one of them, but they all fell short for a number of reasons. Let me first explain the features of this package, and you might see why this one is better (at least what for what I intend to use it for).
Installation
Using Composer
composer require ahfa92/laravel-relation-joins
DISCLAIMER: THIS PACKAGE ONLY FOR FAST PATCHING IF YOU WANT TO HAVE STABLE PACKAGE USE COMPOSER PACKAGE BELOW!!!
composer require reedware/laravel-relation-joins
Versioning
This package was built with the latest version of Laravel in mind, but support goes back to Laravel 5.5.
Usage
1. Performing a join via relationship
This is the entire point of this package, so here's a basic example:
User::query()->joinRelation('posts');
This will apply a join from the User model through the posts relation, leveraging any query scopes (such as soft deletes) automatically.
You can perform joins over all relationship types (except MorphTo, which "has" doesn't support either), including the new "HasOneThrough" relationship. Additionally, you can perform the other types of joins, using a syntax similar to the base query builder:
User::query()->leftJoinRelation('posts'); User::query()->rightJoinRelation('posts'); User::query()->crossJoinRelation('posts');
2. Joining to nested relationships
One of the shining abilities of being able to join through relationships shows up when you have to navigate through a nested web of relationships. When trying to join on a relation through another relation, you can use the "dot" syntax, similar to how the "has" and "with" concepts work:
User::query()->joinRelation('posts.comments');
3. Adding join constraints
This is honestly where I felt a lot of the existing solutions were lacking. They either created custom "where" clauses, or limited the query to only supporting certain types of "where" clauses. With this package, there are no known restrictions, and the means of adding the constraints is very intuitive:
User::query()->joinRelation('posts', function ($join) { $join->where('posts.created_at', '>=', '2019-01-01'); });
This will tack on the specific constraints to the already provided relationship constraints, making this really easy to use. Here are a few more examples:
// Disabling soft deletes for only the "Post" model User::query()->joinRelation('posts', function ($join) { $join->withTrashed(); });
// Using a query scope on the "Post" model User::query()->joinRelation('posts', function ($join) { $join->active(); });
4. Joining through relationships
There are times where you want to tack on clauses for intermediate joins. This can get a bit tricky in some other packages (by trying to automatically deduce whether or not to apply a join, or by not handling this situation at all).
This package introduces something I'm calling a "through" join. Essentially, a "through" join indicates "I want to apply only the final relation in the 'dot' notation to my query".
Here's an example:
// Using a query scope on the "Post" model User::query()->joinRelation('posts', function ($join) { $join->where('is_active', '=', 1); })->joinThroughRelation('posts.comments', function ($join) { $join->where('comments.title', 'like', '%looking for something%'); });
The second part, joinThroughRelation, will only apply the comments relation join, but it will do so as if it came from the Post model.
5. Joining on circular relationships
This package also supports joining on circular relations, and handles it the same way the "has" concept does:
public function employees() { return $this->hasMany(static::class, 'manager_id', 'id'); } User::query()->joinRelation('employees'); // SQL: select * from "users" inner join "users" as "laravel_reserved_0" on "laravel_reserved_0"."manager_id" = "users"."id"
Now clearly, if you're wanting to apply constraints on the employees relation, having this sort of naming convention isn't desirable. This brings me to the next feature:
6. Aliasing joins
You could alias the above example like so:
User::query()->joinRelation('employees as employees'); // SQL: select * from "users" inner join "users" as "employees" on "employees"."manager_id" = "users"."id"
The join doesn't have to be circular to support aliasing. Here's an example:
User::query()->joinRelation('posts as articles'); // SQL: select * from "users" inner join "posts" as "articles" on "articles"."user_id" = "users"."id"
This also works for nested relations:
User::query()->joinRelation('posts as articles.comments as feedback'); // SQL: select * from "users" inner join "posts" as "articles" on "articles"."user_id" = "users"."id" inner join "comments" as "feedback" on "feedback"."post_id" = "articles"."id"
For relations that require multiple tables (i.e. BelongsToMany, HasManyThrough, etc.), the alias will apply to the far/non-pivot table. If you need to alias the pivot/through table, you can use a double-alias:
public function roles() { return $this->belongsToMany(EloquentRoleModelStub::class, 'role_user', 'user_id', 'role_id'); } User::query()->joinRelation('roles as users_roles,roles'); // SQL: select * from "users" inner join "role_user" as "users_roles" on "users_roles"."user_id" = "users"."id" inner join "roles" on "roles"."id" = "users_roles"."role_id" User::query()->joinRelation('roles as users_roles,positions'); // SQL: select * from "users" inner join "role_user" as "position_user" on "position_user"."user_id" = "users"."id" inner join "roles" as "positions" on "positions"."id" = "position_user"."role_id"
7. Everything else
Everything else you would need for joins: aggregates, grouping, ordering, selecting, etc. all go through the already established query builder, where none of that was changed. Meaning I can easily do something like this:
User::query()->joinRelation('licenses')->groupBy('users.id')->orderBy('users.id')->select('users.id')->selectRaw('sum(licenses.price) as revenue');
Personally, I see myself using this a ton in Laravel Nova (specifically lenses), but I've been needing queries like this for years in countless scenarios.
Joins are something that nearly every developer will eventually use, so having Eloquent natively support joining over relations would be fantastic. However, since that doesn't come out of the box, you'll have to install this package instead. My goal with this package is to mirror the Laravel "feel" of coding, where complex implementations (such as joining over named relations) is simple to use and easy to understand.
ahfa92/laravel-relation-joins 适用场景与选型建议
ahfa92/laravel-relation-joins 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「query」 「laravel」 「relation」 「join」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ahfa92/laravel-relation-joins 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ahfa92/laravel-relation-joins 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ahfa92/laravel-relation-joins 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CMS form field for adding has_one relationships using autocomplete
Test easier your Eloquent Models in your Laravel Project
Query filtering in your frontend
Anax Database Active Record module for model classes.
This package introduces the join magic for eloquent models and relations.
Micro Active Record library in PHP, support chain calls, events, and relations.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-07