百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-model-filter

> 后端框架
开源

提供了一种简单的方法,可通过数组参数和查询字符串过滤和搜索精美模型

175 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

提供了一种简单的方法,可通过数组参数和查询字符串过滤和搜索精美模型

laravel-model-filter

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

bash
composer require lacodix/laravel-model-filter

Basic Usage

Filter

Create your first filter

bash
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.

php
// 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

php
// 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();

// Treat %, _ and database-specific wildcard characters as ordinary input
Post::searchLiteral('100%_complete')->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:

php

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

php
protected array $filters = [
    'frontend' => [
        HotFilter::class,
    ],
    'backend' => [
        CreatedAfterFilter::class,
        PublishedFilter::class,
    ]
];

The groups can be used in the scopes

php
Post::filterByQueryString('frontend')->get()

or

php
Post::filter(['hot_filter' => 'hot'], 'frontend')->get();
Post::filter(['created_after_filter' => '2023-01-01'], 'backend')->get();

Preparing Filters for Integrations

Integrations that need to inspect applicable filters before changing a query can use the opt-in FilterPreparation service. It resolves fresh filter objects, applies the same selection, population, normalization, and validation rules as filter(), and returns only filters that are ready to be applied. Preparation itself never changes a query.

php
use Lacodix\LaravelModelFilter\Support\FilterPreparation;

$prepared = (new FilterPreparation)->prepare(
    model: new Post,
    values: $request->all(),
    group: 'backend',
    strictGroup: true,
);

foreach ($prepared as $filter) {
    // Inspect the fresh, populated filter instance.
}

See Opt-in Filter Preparation for strict group resolution, instance configuration, and the PreparedFilters API.

Metadata

Filters can carry free, serializable metadata for whoever renders them - a presentation hint on the filter, a picture or initials per option. The package stores it and never interprets it; filtering and the query string stay untouched.

…

meta() is available on every filter type and merges repeated calls; optionMeta() lives on SelectFilter (and its descendants) and OptionFilter, takes a closure for database-backed options and resolves it once per instance. See Filter Metadata for the contract and the avatar-filter use case.

Testing

bash
composer test

Contributing

Please run the following commands and solve potential problems before committing and think about adding tests for new functionality.

bash
composer rector:test
composer insights
composer csfixer:test
composer phpstan:test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

  • lacodix

License

The MIT License (MIT). Please see License File for more information.

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPeloquentfilterlaravellaravel-package

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架