定制 udaraweerasinghe/query-filter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

udaraweerasinghe/query-filter

最新稳定版本:v1.0.0

Composer 安装命令:

composer require udaraweerasinghe/query-filter

包简介

Dynamic and customizable query filtering for Laravel Eloquent models.

README 文档

README

This service provides a flexible and reusable way to dynamically filter Eloquent queries in Laravel based on structured input. It supports filtering on direct model fields, nested relationships, date conditions, and more.

✨ Features

  • Dynamic filters using field/operator/value structure
  • Supports:
    • where
    • whereIn
    • whereDate
    • whereRelation
    • whereHas (with nested filters)
  • Deep relationship filtering (e.g., author.owner.name)
  • Extendable and clean structure

🤔 How to Use

Controller Example

use App\Models\Post;
use QueryFilter;
use Illuminate\Http\Request;

public function index(Request $request)
{
    $filters = $request->input('filters', []);

    $query = Post::query()->with(['author', 'author.owner']);

    $posts = (new QueryFilter())->apply($query, $filters)->paginate();

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

📂 Example Request Payload or you can set this from controller

{
  "filters": [
    { "field": "title", "operator": "like", "value": "laravel" },
    { "field": "category_id", "operator": "=", "value": 2 },
    { "field": "author.name", "operator": "like", "value": "john" },
    { "field": "published_at", "operator": "date", "value": "2025-04-24" },
    {
      "field": "author",
      "operator": "has",
      "value": [
        { "field": "author.owner.name", "operator": "like", "value": "admin" }
      ]
    }
  ]
}

📅 Supported Operators

OperatorDescription
=Equal to
likePartial match (e.g., %value%)
inArray match using whereIn
dateDate comparison via whereDate
hasNested relationship filter via whereHas
<, >, <=, >=Standard comparison operators

🔧 Extending Support

You can enhance the logic further to include:

  • != (not equal)
  • not in
  • Null checks
  • Between queries

Just modify the applyCondition() method to suit your use case.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-24