PHP code example of ekramhossain / laravel-query-filter

1. Go to this page and download the library: Download ekramhossain/laravel-query-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/ */

    

ekramhossain / laravel-query-filter example snippets


use App\Models\Product;
use EkramHossain\LaravelQueryFilter\Facades\Filter;

public function index(Request $request)
{
    $products = Filter::query(Product::query())
        ->allowedFilters([
            'name',
            'status',
            'price',
            'created_at',
        ])
        ->allowedSorts([
            'name',
            'price',
            'created_at',
        ])
        ->apply($request)
        ->paginate();

    return response()->json($products);
}

->allowedFilters([
    'name',
    'category.name',
])

Product::query()
    ->whereHas('category', function ($query) {
        $query->where(
            'name',
            'like',
            '%electronics%'
        );
    });

->allowedFilters([
    'category.parent.name',
])

->allowedSorts([
    'name',
    'price',
    'created_at',
])

->allowedFilters([
    'name',
    'status',
    'price',
])

->allowedSorts([
    'name',
    'price',
])

use App\Models\Product;
use Illuminate\Http\Request;
use EkramHossain\LaravelQueryFilter\Facades\Filter;

public function index(Request $request)
{
    $products = Filter::query(Product::query())
        ->allowedFilters([
            'name',
            'status',
            'price',
            'manufacture_date',
            'category.name',
        ])
        ->allowedSorts([
            'name',
            'price',
            'manufacture_date',
            'created_at',
        ])
        ->apply($request)
        ->paginate(20);

    return response()->json([
        'success' => true,
        'data' => $products,
    ]);
}
text
config/query-filter.php
text
/api/leaves?filter[issue_date][from]=2026-08-01&filter[issue_date][to]=2026-08-08
sql
WHERE DATE(issue_date) >= '2026-08-01'
AND DATE(issue_date) <= '2026-08-08'
text
/api/products?sort=price
text
/api/products?sort=-price
text
/api/products?sort=-created_at,price