msafadi/laravel-eloquent-join-with
Composer 安装命令:
composer require msafadi/laravel-eloquent-join-with
包简介
Laravel Eloquent Join With Relationships
README 文档
README
Laravel Eloquent Join With is a package that simplifies performing efficient database joins on existing Eloquent relationships of type HasOne and BelongsTo. By utilizing these relationships, JoinWith optimizes performance by executing a single query instead of the two separate queries typically required with the standard with method. This translates to faster and more efficient data retrieval.
Installation
You can install the package via composer:
composer require msafadi/laravel-eloquent-join-with
Usage
There are two ways to use Laravel JoinWith in your application models:
1. Use JoinWith Trait
Include the JoinWith trait provided by the package in your application models:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Safadi\EloquentJoinWith\Database\Concerns\JoinWith; class User extends Model { use JoinWith; // ... other model properties and methods }
With the trait included, you can then use the joinWith method directly on your model queries.
2. Extend the Model Class
Alternatively, you can extend your model classes with Safadi\EloquentJoinWith\Database\Eloquent\Model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Safadi\EloquentJoinWith\Database\Eloquent\Model as JoinWithModel; class User extends JoinWithModel { // ... other model properties and methods }
This approach also grants access to the joinWith method on your model queries.
Usage Examples
Once you've integrated Laravel JoinWith into your models, you can use the joinWith method on your Eloquent model queries.
The defintion and usage of joinWith is exactly the same as using the standard with method. But it will execute a single query joining the parent model and the realted model tables instead of fetching realted models with executing an extra database query. Here's an example:
$user = User::joinWith('profile') ->select('users.id', 'users.name') ->first(); // This will execute a single query joining the users and profiles tables // based on the defined HasOne relationship between User and Profile models.
This code retrieves the user information along with the associated profile's avatar in a single query.
As the standard with method, you can also use nested relations:
$user = User::joinWith('profile.country') ->first(); // This will execute a single query joining the users, profiles, and countries tables // based on the defined HasOne relationship between User and Profile and between Profile and Country models.
For more complex scenarios, you can pass a closure to the joinWith method to customize the join conditions, similar to the standard with method:
$orders = Orders::joinWith(['user' => function ($query) { $query->where('users.status', '=', 'verified'); }]) ->get(); // This will execute a single query joining orders and users tables // based on the BelongsTo relationship and the additional where clause.
This example retrieves orders that belongs to a verified user, combining the user and order information in a single query.
Limitations
- Laravel JoinWith currently works with
HasOneandBelongsTorelationships. Support for other relationship types might be added in future versions. - Specifying which columns of the relationship you would like to retrieve is not supported yet but might be added in future versions.
Contributing
We welcome contributions to this package! If you'd like to contribute, please feel free to open a pull request.
License
This package is distributed under the MIT License. Please see the LICENSE file for more information.
msafadi/laravel-eloquent-join-with 适用场景与选型建议
msafadi/laravel-eloquent-join-with 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.01k 次下载、GitHub Stars 达 165, 最近一次更新时间为 2024 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「performance」 「model」 「laravel」 「Relationships」 「join」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 msafadi/laravel-eloquent-join-with 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 msafadi/laravel-eloquent-join-with 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 msafadi/laravel-eloquent-join-with 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Store your language lines in the database, yaml or other sources
Create link to static resources with cache-breaking segment based on md5 of the file
统计信息
- 总下载量: 6.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 165
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-18