hamidreza-mozhdeh/filter-by-query-string
Composer 安装命令:
composer require hamidreza-mozhdeh/filter-by-query-string
包简介
Filter Laravel Eloquent results by URL query strings and Eloqunet's scopes.
README 文档
README
Please give me a star if you like it.
About Filter by query string
With this package, you can simply use Eloquent scopes to filter the model result by using URL query strings. The advantage of this repository is that you can reuse your local scopes in your business logic (Controllers, Services, etc).
- It's safe
- Reusable local scopes
- Easy to implement
- Customizable
How to install
Run composer require hamidreza-mozhdeh/filter-by-query-string. It will add a FilterByQueryString trait to your project.
How to use
- Add the trait to your models
use FilterByQueryString;.
Category Model:
class Category extends Model
{
use CategoryScopesTrait;
use FilterByQueryString;
}
- In your controller simply pass the Form Request to the Model:
CategoryController:
class CategoryController extends Controller
{
public function index(CategoryRequest $request)
{
return Category::filter($request)->get();
}
CategoryRequest:
class CategoryRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => ['sometimes', 'string', 'min:3', 'max:254'], // A single filter
// Use Associative example
'date-between' => ['sometimes', 'array'], // A filter with two values
'date-between.from' => ['sometimes', 'date'], // Associative array
'date-between.to' => ['sometimes', 'date'], // Associative array
// Or Indexed example
'date-between' => ['sometimes', 'array'], // A filter with two values
'date-between.*' => ['sometimes', 'date'], // Indexed array
];
}
}
- Define your scopes to use the filters
- It's better to have a directory and traits for your models in some where like
App\Models\Traits\Scopes\CategoryScopesTrait.
- It's better to have a directory and traits for your models in some where like
CategoryScopesTrait:
trait CategoryScopesTrait
{
public function scopeName(Builder $query, string $name): Builder
{
return $query->where('name', 'like', "%{$name}%");
}
public function scopeDateBetween(Builder $query, string $from, string $to): Builder
{
return $query->whereBetween('created_at', [$from, $to]);
}
}
- To filter the categories' names for example
osswuse:
http://0.0.0.0/api/categories?name=ossw - To filter with dates use:
Indexed array:http://0.0.0.0/api/categories?date-between[]=2023-11-23&date-between[]=2023-11-25
Associative array:http://0.0.0.0/api/categories?date-between[from]=2023-11-23&date-between[to]=2023-11-25
Customization
$categories = Category::filter(
request: $request,
only: ['name'], // Only accept this filter.
except: ['date-between'], // Do not accept the `date-between` method.
prefix: 'filters', // Add `filters` as array prefix.
requestMethod: 'input' // The default get method is validated but you can choose different one.
);
With prefix: http://0.0.0.0/api/categories?filters[name]=ossw
hamidreza-mozhdeh/filter-by-query-string 适用场景与选型建议
hamidreza-mozhdeh/filter-by-query-string 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hamidreza-mozhdeh/filter-by-query-string 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hamidreza-mozhdeh/filter-by-query-string 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-12-15