承接 yared/laravel-smart-filter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

yared/laravel-smart-filter

Composer 安装命令:

composer require yared/laravel-smart-filter

包简介

Automatically convert HTTP query parameters into Eloquent query filters for Laravel APIs

README 文档

README

A professional Laravel package that automatically converts HTTP query parameters into Eloquent query filters. Eliminate repetitive filtering logic in your Laravel APIs.

Instead of writing manual filtering logic in controllers, simply write:

Products list with filter buttons and results table Screenshot: Products page with quick filter links (All, Price > 100, Search, Category, Sort, etc.) and a table displaying filtered results.

Product::filter()->get();

The package automatically parses request query parameters and applies filters.

Installation

composer require yared/laravel-smart-filter

The package supports Laravel auto-discovery. For manual registration, add the service provider to config/app.php:

'providers' => [
    // ...
    Yared\SmartFilter\SmartFilterServiceProvider::class,
],

Publish Configuration (Optional)

php artisan vendor:publish --tag=smart-filter-config

Quick Start

1. Add the Trait to Your Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Yared\SmartFilter\Traits\HasSmartFilter;

class Product extends Model
{
    use HasSmartFilter;
}

2. Use in Your Controller

<?php

namespace App\Http\Controllers;

use App\Models\Product;

class ProductController extends Controller
{
    public function index()
    {
        return Product::filter()->paginate();
    }
}

That's it! Your API now supports automatic filtering from query parameters.

Before vs After: Manual filtering code vs single line with Smart Filter Before/After: Compare verbose manual filtering logic in controllers vs. the single-line Product::filter()->paginate() approach.

Supported Filter Types

Basic Equality

/api/products?category=phone

Comparison Operators

Parameter Example SQL Equivalent
Greater than ?price>100 WHERE price > 100
Less than ?price<500 WHERE price < 500
Greater or equal ?price>=100 WHERE price >= 100
Less or equal ?price<=500 WHERE price <= 500
Not equal ?status!=draft WHERE status != 'draft'

Range Filters

/api/products?price_between=100,500

Applies WHERE price BETWEEN 100 AND 500.

Sorting

/api/products?sort=-price    # Descending order
/api/products?sort=price     # Ascending order
/api/products?sort=-price,name  # Multiple columns

Search

/api/products?search=iphone

Applies LIKE %iphone% across configurable searchable columns (default: name, description).

Relationship Filtering

Filter by related model attributes using dot notation:

/api/products?category.name=electronics

Internally converts to:

$query->whereHas('category', function ($q) {
    $q->where('name', 'electronics');
});

Supports nested relationships: ?category.parent.name=electronics

Natural Language Filtering

Human-readable filter syntax:

/api/products?filter=price > 100 and stock > 0

Supported operators: >, <, >=, <=, =, !=
Logical operators: and, or

JSON Column Filtering

/api/products?attributes->color=red

Pagination

/api/products?page=2&per_page=10

Debug Mode

Add debug=true to see what filters were applied:

/api/products?price>100&category=phone&debug=true

Response includes metadata:

{
    "data": [...],
    "filters_applied": [
        "price > 100",
        "category = phone"
    ],
    "sorting": "price DESC"
}

For debug metadata with pagination, use filterAndPaginate():

return Product::filterAndPaginate();

Debug mode response showing filters_applied and sorting metadata Debug mode: API response with filters_applied and sorting metadata when ?debug=true is used.

Configuration

Publish the config file and customize:

// config/smart-filter.php

return [
    'searchable' => ['name', 'description'],
    'reserved' => ['page', 'per_page', 'sort', 'search', 'filter', 'debug'],
    'range_suffix' => '_between',
];

Model-Specific Searchable Columns

Override searchable columns per model:

Option 1: Property

class Product extends Model
{
    use HasSmartFilter;

    protected array $searchable = ['name', 'description', 'sku'];
}

Option 2: Method

class Product extends Model
{
    use HasSmartFilter;

    public function getSearchableColumns(): array
    {
        return ['name', 'description', 'sku'];
    }
}

Option 3: Inline Options

Product::filter(request(), ['searchable' => ['name', 'sku']])->get();

API Usage Examples

Request Description
/api/products?price>100 Price greater than 100
/api/products?price>100&price<500 Price between 100 and 500
/api/products?category=phone Category equals phone
/api/products?category.name=electronics Related category name
/api/products?search=iphone Search in name/description
/api/products?sort=-price Sort by price descending
/api/products?filter=price > 100 and stock > 0 Natural language
/api/products?price_between=100,500 Range filter
/api/products?attributes->color=red JSON column
/api/products?price>100&debug=true With debug metadata

Architecture

The package follows clean architecture with separated responsibilities:

  • QueryParser - Parses HTTP query parameters into structured filter data
  • FilterBuilder - Applies parsed filters to Eloquent queries
  • QueryDebugger - Records and outputs applied filters for debugging

Architecture flow: HTTP Request → QueryParser → FilterBuilder → Eloquent Architecture: Request with query params flows through QueryParser, FilterBuilder, and into Eloquent.

Facade (Optional)

Register the facade in config/app.php:

'aliases' => [
    'SmartFilter' => Yared\SmartFilter\Facades\SmartFilter::class,
],

Usage:

use SmartFilter;

$query = Product::query();
SmartFilter::apply($query);
$products = $query->get();

Requirements

  • PHP 8.1+
  • Laravel 9.x, 10.x, 11.x, or 12.x

License

MIT License

yared/laravel-smart-filter 适用场景与选型建议

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

它主要适用于以下技术方向: 「api」 「filter」 「query」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-08