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.
- β‘ 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
- PHP 8.1 or higher
- Laravel 10.x, 11.x, or 12.x
composer require sharifuddin/laravel-smart-filterphp artisan vendor:publish --provider="Sharifuddin\\LaravelSmartFilter\\SmartFilterServiceProvider" --tag="smart-filter-config"<?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,
],
];
}
}$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();// 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();$filters = [
'category.name' => ['value' => 'Electronics', 'operator' => 'like', 'type' => 'string'],
'brand.name' => ['value' => 'Apple', 'operator' => 'like', 'type' => 'string'],
];
$products = Product::applySmartFilters($filters)->get();<?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'));
}
}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,
],
];# Run all tests
composer test
# With coverage
composer test-coverageExample 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));
}public function scopeApplySmartFilters(
Builder $query,
array $filters = [],
array $options = []
): Builderpublic function scopeFilterFromRequest(
Builder $query,
array $allowedFilters = [],
array $options = []
): Builder- Use indexed columns for frequent filters
- Prefer exact (
=) overLIKEfor faster queries - Limit filters using config:
max_filters - Always paginate large results
Contributions are welcome!
See CONTRIBUTING.md for details.
This package is open-sourced software licensed under the MIT License.
Laravel Smart Filter β Powerful, Relation-Aware Filtering for Eloquent. β‘
GitHub β’ Packagist β’ Issues β’ Discussions