PHP code example of sharifuddin / laravel-smart-filter

1. Go to this page and download the library: Download sharifuddin/laravel-smart-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sharifuddin / laravel-smart-filter example snippets




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);

$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();



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'));
    }
}

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,
    ],
];

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 = []
): Builder

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