lacodix/laravel-model-filter
Composer 安装命令:
composer require lacodix/laravel-model-filter
包简介
A Laravel package to filter, search and sort models with ease while fetching from database.
README 文档
README
This package allows you to filter, search and sort models while fetching from database with ease. It contains additional functionality to use query strings to filter, search and sort.
With this package you can easily filter, search and sort your Eloquent models. It supports various filter types like strings, dates, numbers, and enums out of the box. You can also create complex custom filters to handle any specific database logic.
Additionally you can use the visualisation functionality of filters.
Documentation
You can find the entire documentation for this package on our documentation site
See our Upgrade Guide for information on how to upgrade from older versions.
Installation
composer require lacodix/laravel-model-filter
Basic Usage
Filter
Create your first filter
php artisan make:filter CreatedAfterFilter --type=date --field=created_at
Filters can be applied directly to models, but they can also be easily applied to relations and nested relations
using the RunsOnRelation trait. This automatically wraps the filter logic in a whereHas closure and correctly
qualifies the field names using the related table.
// Set the filter mode // App\Models\Filters\CreatedAfterFilter public FilterMode $mode = FilterMode::GREATER_OR_EQUAL; // Apply this filter and the HasFilters trait to a Model // App\Models\Post use HasFilters; protected array $filters = [ CreatedAfterFilter::class, ]; // Somwhere in a controller, select all posts created after 1st of January 2023 Post::filter(['created_after_filter' => '2023-01-01'])->get(); // Do the same via query string by calling // this url: https://.../posts?created_after_filter=2023-01-01 Post::filterByQueryString()->get();
Search
// add searchable fields and the IsSearchable trait to Model: // App\Models\Post use IsSearchable; protected array $searchable = [ 'title', 'content', ]; // Somewhere in controller, find all posts that contain "test" in title or content Post::search('test')->get(); // Do the same via query string by calling // this url: https://.../posts?search=test Post::searchByQueryString()->get();
Visualize
All filters have a blade template that can visualize the filter with one or multiple input fields. To visualize all filters of a dedicated model you can use a blade component:
<x-lacodix-filter::model-filters :model="Post::class" />
Grouping
Sometimes you don't need all of the filters for all parts of a web application. Maybe there shall be different filters be available to the backend as in the frontend, or different user types shall be able to use different filters.
For such cases this package offers filter grouping when adding filters to models
protected array $filters = [ 'frontend' => [ HotFilter::class, ], 'backend' => [ CreatedAfterFilter::class, PublishedFilter::class, ] ];
The groups can be used in the scopes
Post::filterByQueryString('frontend')->get()
or
Post::filter(['hot_filter' => 'hot'], 'frontend')->get(); Post::filter(['created_after_filter' => '2023-01-01'], 'backend')->get();
Testing
composer test
Contributing
Please run the following commands and solve potential problems before committing and think about adding tests for new functionality.
composer rector:test composer insights composer csfixer:test composer phpstan:test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
lacodix/laravel-model-filter 适用场景与选型建议
lacodix/laravel-model-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58k 次下载、GitHub Stars 达 175, 最近一次更新时间为 2023 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「filter」 「model」 「laravel」 「filters」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lacodix/laravel-model-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lacodix/laravel-model-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lacodix/laravel-model-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Nova searchable filter for belongsTo relationships.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Abstraction Layer to index and search entities
统计信息
- 总下载量: 58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 175
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-17