PHP code example of joggapp / laravel-query-filters

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

    

joggapp / laravel-query-filters example snippets


protected array $sortableFields = [
    'name',
    'price',
];

protected array $defaultFilters = [
    'sort_by' => 'price:asc,name:asc'
];

public function category(string|array $value)
{
    if (is_array($value)) {
        $query->whereIn('category', $value);
    } else {
        $query->where('category', $category);
    }
}

use App\Model\Product;
use App\QueryFilters\ProductQueryFilter;

$filters = [
    'category' => 'Apparel',
    'sort_by' => 'price:asc,name:asc',
];

(new ProductQueryFilter())
    ->set($filters)
    ->apply(Product::query())
    ->get();

use App\Models\Product;
use App\QueryFilters\ProductQueryFilter;
use Illuminate\Support\Facades\Validator;
use JoggApp\LaravelQueryFilters\Rules\SortBy;

$data = [
    'sort_by' => 'price:asc,name:asc',
];

$validator = Validator::make($data, [
    'sort_by' => ['sometimes', new SortBy()],
]);

$validated = $validator->validated();

$products = (new ProductQueryFilter())
    ->set($validated)
    ->apply(Product::query())
    ->get();