jedrzej/withable
Composer 安装命令:
composer require jedrzej/withable
包简介
Withable trait for Laravel's Eloquent models
README 文档
README
This package adds possibility to dynamically eager load Eloquent models relations in Laravel 4/5/6 using request parameters.
You could also find those packages useful:
- Searchable - Allows filtering your models using request parameters
- Sortable - Allows sorting your models using request parameters
- Pimpable - A meta package that combines Sortable, Searchable and Withable behaviours
Composer install
Add the following line to composer.json file in your project:
"jedrzej/withable": "0.0.6"
or run the following in the commandline in your project's root folder:
composer require "jedrzej/withable" "0.0.6"
Setting up withable models
In order to make an Eloquent model withable, add the trait to the model and define a list of relations that can be eagerly loaded using request paramaters.
You can either define a $withable property or implement a getWithableRelations method if you want to execute some logic to define
list loadable relations.
use Jedrzej\Withable\WithableTrait; class Post extends Eloquent { use WithableTrait; // either a property holding a list of loadable relations... protected $withable = ['owner', 'forum']; // ...or a method that returns a list of loadable relations protected function getWithableRelations() { return ['owner', 'forum']; } }
In order to make all relations loadable put an asterisk * in the list:
protected $withable = ['*'];
Loading relations
WithableTrait adds a withRelations() scope to the model - you can pass it a list of relations to load as you would pass to Eloquent's with() method:
// return all posts with the user who created them and forum where they were posted Post::withRelations(['owner', 'forum'])->get(); // return all posts with the user who created them Post::withRelations('owner')->get();
or it will use Request::all() as default. In the URL you can pass a list of relations to load or a single relation in the with parameter:
// return all posts with the user who created them and forum where they were posted by appending to the URL ?with[]=owner&with[]=forum // return all posts with the user who created them ?with=owner //and then calling Post::withRelations()->get();
Loading scoped relations
If you have defined local scopes in a related model, you can load this relation with the scope applied. E.g. in order to eagerly load posts with their approved comments:
// define local scope in Comment model public function scopeApproved($query) { $query->whereIsApproved(true); } // append the following to the URL in order to load "comments" relation with "approved" local scope applied ?with=comments:approved // load the posts in your controller Post::withRelations()->get();
Additional configuration
If you are using with request parameter for other purpose, you can change the name of the parameter that will be
interpreted as a list of relations to load by setting a $withParameterName property in your model, e.g.:
protected $withParameterName = 'relations';
If you need to execute additional logic to define a list of relations to load (e.g. permission checking),
you can implement getWithRelationsList() method in your model and make that return list of relations:
public function getWithRelationsList() { return Request::get('relations'); }
jedrzej/withable 适用场景与选型建议
jedrzej/withable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 203.68k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2015 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「request」 「laravel」 「eloquent」 「load」 「eager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jedrzej/withable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jedrzej/withable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jedrzej/withable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Adds request-parameter validation to the SLIM 3.x PHP framework
A simple HTTP application builder using PSR-15 HTTP Server Request Handler and Middleware.
Marussia request
Alfabank REST API integration
统计信息
- 总下载量: 203.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 35
- 依赖项目数: 2
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-17