sharifuddin/laravel-smart-filter

Advanced smart filtering for Laravel Eloquent models with relation support and multiple filter types

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/sharifuddin/laravel-smart-filter

v1.0.0 2025-11-01 14:44 UTC

This package is auto-updated.

Last update: 2025-11-02 09:02:28 UTC


README

Laravel Smart Filter

Latest Version Total Downloads Build Status License PHP Version Laravel Version

๐Ÿš€ Introduction

Laravel Smart Filter is a powerful and flexible filtering engine for Eloquent models.
It enables dynamic, request-driven filtering across columns and relationships โ€” with support for multiple operators, data types, and nested relations.

โœจ Features

  • โšก Dynamic Filtering โ€” Filter data using arrays or request parameters
  • ๐Ÿ” Relation Filtering โ€” Automatically filters related models
  • ๐Ÿงฉ Multiple Operators โ€” =, !=, >, <, like, in, between, null, etc.
  • โš™๏ธ Type-Aware Parsing โ€” Handles string, numeric, boolean, and date types
  • ๐Ÿ”„ Request Integration โ€” Parse filters directly from HTTP requests
  • ๐Ÿงช Fully Tested & Configurable โ€” Comprehensive test coverage and flexible config

๐Ÿ“ฆ Installation

Requirements

  • PHP 8.1 or higher
  • Laravel 10.x, 11.x, or 12.x

Install via Composer

composer require sharifuddin/laravel-smart-filter

(Optional) Publish Configuration

php artisan vendor:publish --provider="Sharifuddin\\LaravelSmartFilter\\SmartFilterServiceProvider" --tag="smart-filter-config"

๐ŸŽฏ Quick Start

1๏ธโƒฃ Add the Trait to a Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sharifuddin\LaravelSmartFilter\Traits\SmartFilter;

class Product extends Model
{
    use SmartFilter;

    public function getFilterableFields(): array
    {
        return [
            'name' => ['operator' => 'like', 'type' => 'string'],
            'price' => ['operator' => 'between', 'type' => 'float'],
            'category_id' => ['operator' => 'in', 'type' => 'integer'],
            'is_active' => ['operator' => '=', 'type' => 'boolean'],
            'created_at' => ['operator' => 'date', 'type' => 'date'],
        ];
    }

    public function getFilterableRelations(): array
    {
        return [
            'category' => [
                'fields' => ['name', 'slug'],
                'max_depth' => 1,
            ],
            'brand' => [
                'fields' => ['name', 'description'],
                'max_depth' => 1,
            ],
        ];
    }
}

๐Ÿงฉ Usage Examples

๐Ÿ”น Basic Filtering

$filters = [
    'name' => ['value' => 'Laptop', 'operator' => 'like', 'type' => 'string'],
    'price' => ['value' => [100, 1000], 'operator' => 'between', 'type' => 'float'],
    'is_active' => ['value' => true, 'operator' => '=', 'type' => 'boolean'],
];

$products = Product::applySmartFilters($filters)->get();

๐Ÿ”น Filter from HTTP Request

// Example URL: /products?name=Laptop&price_min=100&price_max=1000&category_id=1,2,3

$products = Product::filterFromRequest()->paginate(20);

specify allowed filters:

$products = Product::filterFromRequest([
    'name' => ['operator' => 'like', 'type' => 'string'],
    'price' => ['operator' => 'between', 'type' => 'float'],
    'category_id' => ['operator' => 'in', 'type' => 'integer'],
])->get();

๐Ÿ”น Relation Filtering

$filters = [
    'category.name' => ['value' => 'Electronics', 'operator' => 'like', 'type' => 'string'],
    'brand.name' => ['value' => 'Apple', 'operator' => 'like', 'type' => 'string'],
];

$products = Product::applySmartFilters($filters)->get();

๐Ÿ”น Combined Example (Controller)

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index(Request $request)
    {
        $products = Product::filterFromRequest([
                'name' => ['operator' => 'like', 'type' => 'string'],
                'price' => ['operator' => 'between', 'type' => 'float'],
                'category_id' => ['operator' => 'in', 'type' => 'integer'],
                'is_active' => ['operator' => '=', 'type' => 'boolean'],
            ])
            ->with(['category', 'brand'])
            ->orderBy('created_at', 'desc')
            ->paginate(24);

        return view('products.index', compact('products'));
    }
}

โš™๏ธ Configuration

config/smart-filter.php

return [
    'defaults' => [
        'deep' => true,
        'max_relation_depth' => 2,
        'case_sensitive' => false,
        'strict_mode' => false,
    ],

    'fields' => [
        'excluded' => ['id', 'created_at', 'updated_at', 'deleted_at', 'password'],
        'default_operators' => [
            'string' => 'like',
            'integer' => '=',
            'float' => '=',
            'boolean' => '=',
            'date' => '=',
            'array' => 'in',
        ],
    ],

    'request' => [
        'prefix' => '',
        'array_delimiter' => ',',
        'date_format' => 'Y-m-d',
    ],

    'performance' => [
        'max_join_tables' => 5,
        'query_timeout' => 30,
        'max_filters' => 20,
    ],
];

๐Ÿงช Testing

# Run all tests
composer test

# With coverage
composer test-coverage

Example test:

public function test_filters_products_by_price_and_status()
{
    $filters = [
        'price' => ['value' => [100, 500], 'operator' => 'between', 'type' => 'float'],
        'is_active' => ['value' => true, 'operator' => '=', 'type' => 'boolean'],
    ];

    $products = Product::applySmartFilters($filters)->get();

    $this->assertTrue($products->every(fn($p) => $p->is_active));
}

๐Ÿง  API Reference

โžค scopeApplySmartFilters()

public function scopeApplySmartFilters(
    Builder $query,
    array $filters = [],
    array $options = []
): Builder

โžค scopeFilterFromRequest()

public function scopeFilterFromRequest(
    Builder $query,
    array $allowedFilters = [],
    array $options = []
): Builder

๐Ÿงฐ Performance Tips

  • Use indexed columns for frequent filters
  • Prefer exact (=) over LIKE for faster queries
  • Limit filters using config: max_filters
  • Always paginate large results

๐Ÿค Contributing

Contributions are welcome!
See CONTRIBUTING.md for details.

๐Ÿ“„ License

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

Laravel Smart Filter โ€” Powerful, Relation-Aware Filtering for Eloquent. โšก

GitHub โ€ข Packagist โ€ข Issues โ€ข Discussions