ayman-els/laravel-filter-package
Composer 安装命令:
composer require ayman-els/laravel-filter-package
包简介
A Laravel package with a custom filtering trait
README 文档
README
Filter your queries based on url query string parameters like a breeze.
Compatible with Laravel 5.x 6.x 7.x 8.x 9.x 10.x.
Table of Content
Describing the Problem
You have probably faced the situation where you needed to filter your query based on given parameters in url query-string and after developing the logics, You've had such a code:
$users = User::latest(); if(request('username')) { $users->where('username', request('username')); } if(request('age')) { $users->where('age', '>', request('age')); } if(request('email')) { $users->where('email', request('email')); } return $users->get();
This works, But it's not a good practice.
When the number of parameters starts to grow, The number of these kind of if statements also grows and your code gets huge and hard to maintain.
Also it's against the Open/Closed principal of SOLID principles, Because when you have a new parameter, You need to get into your existing code and add a new logic (which may breaks the existing implementations).
So we have to design a way to make our filters logics separated from each other and apply them into the final query, which is the whole idea behind this package.
Usage
- First you need to install the package:
$ composer require ayman-els/laravel-filter-package
- Then you should
usetheFiltertrait in your model, And define$filterCols, $filterDates, $filterSearchCols, $filterColsChildsproperty.
use AymanEls\LaravelFilterPackage\Traits\Filter; class Product extends Model { use Filter; public static $filterCols = ['category_id', 'department_id', 'user_id']; public static $filterDates = ['from' => 'created_at', 'to' => 'created_at']; public static $filterSearchCols = ['name_en', 'name_ar', 'description']; public static $filterColsChilds = ['currency' => 'department']; ... }
- You need to use
filter()method in your eloquent query. For example:
Product::filter()->get();
assume our query is something like this:
?search=field&from=2023-01-01&to=2023-01-02&category_id=1&department_id=1&user_id=1¤cy=1
ayman-els/laravel-filter-package 适用场景与选型建议
ayman-els/laravel-filter-package 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 100 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「filter」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ayman-els/laravel-filter-package 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ayman-els/laravel-filter-package 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ayman-els/laravel-filter-package 相关的其它包
同方向 / 同关键字的高下载量 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.
Abstraction Layer to index and search entities
Symfony DataGridBundle
Data provider for yii2
统计信息
- 总下载量: 100
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-15