makidizajnerica/laravel-filterator
Composer 安装命令:
composer require makidizajnerica/laravel-filterator
包简介
Simple search filtering manager for models.
README 文档
README
Simple search filtering manager for models.
Installation
composer require makidizajnerica/laravel-filterator
Usage
Your model needs to implement \MakiDizajnerica\Filterator\Contracts\Filterable. Next define filters method that will return filters for the model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use MakiDizajnerica\Filterator\Filter; use MakiDizajnerica\Filterator\Contracts\Filterable as FilterableContract; class User extends Model implements FilterableContract { // ... /** * Get filters for the filterator manager. * * @return array<string, \MakiDizajnerica\Filterator\Filters\Filter> */ public function filters(): array { return [ 'name' => Filter::string(fn (Builder $query, string $name) => $query->where('name', 'LIKE', "%{$name}%")), ]; } }
Array key will be the query param name inside the request.
There is also a couple of available filters:
<?php namespace App\Models; use Illuminate\Support\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use MakiDizajnerica\Filterator\Filter; use MakiDizajnerica\Filterator\Contracts\Filterable as FilterableContract; class User extends Model implements FilterableContract { // ... /** * Get filters for the filterator manager. * * @return array<string, \MakiDizajnerica\Filterator\Filters\Filter> */ public function filters(): array { return [ 'is_admin' => Filter::boolean(fn (Builder $query, bool $isAdmin) => $query->where('is_admin', $isAdmin)), 'created_at' => Filter::date(fn (Builder $query, Carbon $createdAt) => $query->whereDate('created_at', $createdAt)) ->format('Y-m-d') // optionally define format ->timezone('Europe/Belgrade'), // optionally define timezone 'credit' => Filter::float(fn (Builder $query, float $credit) => $query->where('credit', $credit)) ->decimals(2), // optionally define number of decimals 'age' => Filter::integer(fn (Builder $query, int $age) => $query->where('age', $age)), 'name' => Filter::string(fn (Builder $query, string $name) => $query->where('name', 'LIKE', "%{$name}%")), ]; } }
You can also define default closure for the filter. Default closure gets called when query param is not set.
<?php namespace App\Models; use Illuminate\Support\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use MakiDizajnerica\Filterator\Filter; use MakiDizajnerica\Filterator\Contracts\Filterable as FilterableContract; class User extends Model implements FilterableContract { // ... /** * Get filters for the filterator manager. * * @return array<string, \MakiDizajnerica\Filterator\Filters\Filter> */ public function filters(): array { return [ // ... 'created_at' => Filter::date(fn (Builder $query, Carbon $createdAt) => $query->whereDate('created_at', $createdAt)) ->default(fn (Builder $query) => $query->whereDate('created_at', '1985-05-05')), // ... ]; } }
Next, when you want to filter your model, you can do it like so:
<?php use App\Models\User; use MakiDizajnerica\Filterator\Facades\Filterator; $users = filterator(User::class)->get(); // or $users = Filterator::filter(User::class)->get();
Return type of the filterator method is \Illuminate\Database\Eloquent\Builder so you can chain other query methods.
Adding new filters
Todo
Author
Nemanja Marijanovic (n.marijanovic@hotmail.com)
Licence
Copyright © 2023, Nemanja Marijanovic n.marijanovic@hotmail.com
All rights reserved.
For the full copyright and license information, please view the LICENSE file that was distributed within the source root of this package.
makidizajnerica/laravel-filterator 适用场景与选型建议
makidizajnerica/laravel-filterator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 105 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「search」 「filter」 「model」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 makidizajnerica/laravel-filterator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makidizajnerica/laravel-filterator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 makidizajnerica/laravel-filterator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Pimcore 10.x Website Indexer (powered by Zend Search Lucene)
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 105
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-12