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
Requires
- php: ^8.1
- laravel/framework: ^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
๐ 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 (
=) overLIKEfor 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