webdevjohn/filterable
Composer 安装命令:
composer require webdevjohn/filterable
包简介
Filterbale eloquent models for Laravel
README 文档
README
Introduction
The Filterable package was created to provide an easy way to filter Laravel Eloquent models.
Purpose
- To provide an easy way to dynamically filter an Eloquent model via HTTP GET and POST methods.
- To provide an easy way to manually filter an Eloquent model via a method call.
- To encapsulate filter logic within its own dedicated class.
- To allow filter logic to be re-used between Eloquent models.
- To follow modern OOP coding standards and design patterns.
How to works
The package uses a factory to determine which filters to apply to a model. The factory accepts one argument through the constructor, a filter component. The filter component is used to instruct the Filter Factory on which filters to instantiate. This approach allows you to have many different instances of a Filter Factory using different components that change its behaviour.
The request input is used to build the filters for a particular model. For example, the query string parameter "year_released" will instantiate the "YearReleasedFilter" class and apply the filter to the model.
Installation
composer require webdevjohn/filterable
Configuration
Complete the following steps.
- Step 1: Create and configure the filter component.
- Step 2: Filter Factory setup.
- Step 3: Create the filters.
- Step 4: Add the Filterable trait to the model that you want to filter.
Step 1 - Create and configure the filter component
The purpose of the filter component is to instruct the Filter Factory on which filters to instantiate. The list of filter that can be instantiated for a particular model are listed in the getInstantiableFilters() method. These can also be organised by modified the namespace that is returned by the getInstantiableFiltersNamespace() method.
To create a filter component from the command line of your Laravel project, type the Artisan command below, specifying the name of the component.
php artisan filter:component {NameOfComponent}
You are free to name your filter component classes however you choose. The only requirement of a filter component is that it must implement the FilterComponentInterface.
The newly created filter component will be placed in the App\Filters\Components directory and namespace by default.
You can override the default directory \ namespace by passing an optional 2nd argument.
php artisan filter:component {NameOfComponent} {namespace}
Step 2 - Filter Factory setup
The filter component created in step 1 needs to be injected into the constructor argument of the FilterFactory. You can do this by binding a filter factory instance and injecting the filter component.
Open the App/Providers/AppServiceProvider.php file and create a new binding for the FilterFactory in the register() method, specifying an alias for the Factory. Then pass the component into the FilterFactory constructor using the $app->make()method.
You can use the code below as a template, replacing “FactoryAlias” and “YourComponent”.
App/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('FactoryAlias', function ($app) {
return new \Webdevjohn\Filterable\FilterFactory(
$app->make(\App\Filters\Components\YourComponent::class)
);
});
}
Step 3- Create Filters
The filters that are created will be put in the App\Filters directory and namespace by default.
php artisan filter:make {FilterName}
You can override the default directory \ namespace by passing an optional 2nd argument
php artisan filter:make {FilterName} {namespace}
Once a filter has been created for a particular model, you will need to update the getInstantiableFilters() method on the filter component so that the factory can create the filter.
Step 4- The Filterable Trait
Add the Filterable trait to the model that you want to filter:
use Webdevjohn\Filterable\Traits\Filterable;
The filterable trait exposes the getFilterFactory() method to the model and is used to retrieve the FilterFactory instance from the IOC (inversion of control) container.
public function getFilterFactory(string $factory)
{
return app()->make($factory);
}
You also need to add the code snippet below to your model, replacing 'YourFilterFactory' with the name (alias) of the factory that was setup in step 2
The scopeFilters method provides easy access to the FilterFactory that was setup in the IOC container in Step 2.
public function scopeFilters($query, $request)
{
return $this->getFilterFactory('YourFilterFactory')->make($query, $request);
}
Usage
The example below is calling the Filters() method from a repository, but the concept is that same if used directly in a model or controller.
With any of your existing queries you can call the Filters() method, passing through the $request->input(). This will dynamically instantiate and apply the filters to the model.
public function getLatestTracks($request)
{
return $this->model->WithRelations()
->Filters($request->input())
->orderBy('purchase_date', 'DESC')
->take(12)
->get()
}
webdevjohn/filterable 适用场景与选型建议
webdevjohn/filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 101 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「query」 「model」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webdevjohn/filterable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webdevjohn/filterable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webdevjohn/filterable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
Interfaces for web resource models and services to retrieve and create them
Anax Database Active Record module for model classes.
Symfony DataGridBundle
统计信息
- 总下载量: 101
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-15