lotous-organization/laravel-filter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lotous-organization/laravel-filter

Composer 安装命令:

composer require lotous-organization/laravel-filter

包简介

A Laravel package for filterable traits

README 文档

README

usermp-laravel-filterable

Overview

The Filterable trait is designed to be used within Eloquent models in a Laravel application. It provides a convenient way to apply filters to Eloquent queries based on HTTP request parameters. This trait supports filtering by model attributes as well as by attributes of related models, using various operators.

Installation

  1. Add the package to your project using Composer:

    composer require lotous-organization/laravel-filter
  2. Add the Filterable trait to your Eloquent model:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use LotousOrganization\LaravelFilter\Traits\Filterable; // Correct path to the Trait
    
    class YourModel extends Model
    {
        use Filterable;
    
        // (Optional) Override the main request key containing the filters.
        // protected string $filterRequestKeyOverride = 'my_filters'; // Defaults to 'filter'
    
        // Attributes of this model that can be filtered
        protected $filterable = [
            'attribute1',
            'attribute2',
            'status',
            'created_at',
            // Add other filterable attributes
        ];
    
        // Names of relations whose attributes can be filtered
        protected $filterableRelations = [
            'relation1', // Example: for filtering relation1.name
            'user',
            // Add other filterable relations
        ];
    }

Usage

To use the Filterable trait, call the filter scope on your model query and pass the Illuminate\Http\Request object. Filters should be passed in the query string under a main key, which defaults to filter.

Example in a Controller:

namespace App\Http\Controllers;

use App\Models\YourModel;
use Illuminate\Http\Request; // Inject the Request object

class YourModelController extends Controller
{
    public function index(Request $request)
    {
        $results = YourModel::query()
            ->filter($request) // Pass the entire Request object
            ->paginate();

        return response()->json($results);
    }
}

Examples

All filter parameters should be nested under a main key in the query string. The default key is filter.

1. Basic Attribute Filtering

Assume the Post model has title and status in its $filterable array.

  • Filter by title (implicitly uses 'like'): GET /posts?filter[title]=Example Post (Finds posts where title contains "Example Post")

  • Filter by status using 'equal' operator: GET /posts?filter[status][equal]=published (Finds posts where status is exactly "published")

  • Filter by creation date using 'gte' (greater than or equal to): GET /posts?filter[created_at][gte]=2023-01-01

2. Filtering by Related Model Attributes

Assume the Post model has user in $filterableRelations and the User model has a name attribute.

  • Filter posts by user's name (implicitly 'like'): GET /posts?filter[user.name]=John Doe

  • Filter posts by user's email using 'equal': GET /posts?filter[user.email][equal]=john.doe@example.com

3. Using Specific Operators

  • in operator (for multiple values): Filter posts with status 'published' OR 'pending': GET /posts?filter[status][in]=published,pending Alternatively, using array syntax for the in values: GET /posts?filter[status][in][]=published&filter[status][in][]=pending

    Filter posts belonging to users with specific IDs: GET /posts?filter[user.id][in]=1,5,10

  • between operator (for ranges): Filter posts created between two dates: GET /posts?filter[created_at][between][]=2023-01-01&filter[created_at][between][]=2023-12-31

  • null / notnull operators: Filter posts where published_at is NULL: GET /posts?filter[published_at][null]

    Filter posts where updated_at is NOT NULL: GET /posts?filter[updated_at][notnull]

4. Customizing the Main Filter Key

If you defined $filterRequestKeyOverride = 'my_query_filters'; in your model:

GET /posts?my_query_filters[title]=My%20Post

Supported Operators

The following operators can be used by specifying them as a key for the filter value:

Operator Query String Example Description
(none) filter[title]=word Default: LIKE '%word%' for string values.
equal filter[status][equal]=active Exact match (=).
notequal filter[status][notequal]=archived Not equal (!=).
gt filter[views][gt]=100 Greater than (>).
gte filter[views][gte]=100 Greater than or equal to (>=).
lt filter[stock][lt]=10 Less than (<).
lte filter[stock][lte]=10 Less than or equal to (<=).
like filter[description][like]=important LIKE '%important%'.
notlike filter[description][notlike]=trivial NOT LIKE '%trivial%'.
startswith filter[sku][startswith]=ABC LIKE 'ABC%'.
endswith filter[filename][endswith]=.pdf LIKE '%.pdf'.
in filter[id][in]=1,2,3
filter[id][in][]=1&filter[id][in][]=2
Matches any of the comma-separated values.
notin filter[category][notin]=old,deprecated Does not match any of the comma-separated values.
between filter[date][between][]=2023-01-01&filter[date][between][]=2023-01-31 Value is between two specified values.
notbetween filter[price][notbetween][]=100&filter[price][notbetween][]=200 Value is not between two specified values.
null filter[deleted_at][null] Value is NULL.
notnull filter[confirmed_at][notnull] Value is NOT NULL.

Note on in, notin, between, notbetween: For between and notbetween, the values must be provided as an array in the query string as shown. For in and notin, values can be a comma-separated string or an array.

License

This package is open-sourced software licensed under the MIT license.

lotous-organization/laravel-filter 适用场景与选型建议

lotous-organization/laravel-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lotous-organization/laravel-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lotous-organization/laravel-filter 我们能提供哪些服务?
定制开发 / 二次开发

基于 lotous-organization/laravel-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-09