1. Go to this page and download the library: Download alifcoder/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/ */
alifcoder / query-filter example snippets
namespace App\Filters;
use Alif\QueryFilter\Abstracts\BaseEBFilter;
use Alif\QueryFilter\Enums\FilterOperator;
use Alif\QueryFilter\Field;
class ProductFilter extends BaseEBFilter
{
protected static array $with = ['category']; // Eager load the category with each product result.
/** Expose only the product fields that clients may query. */
protected function fields(): array
{
return [
'id' => Field::make('id')
->operators([FilterOperator::Equal, FilterOperator::In])
->searchable(false),
'name' => Field::make('name')
->operators([
FilterOperator::Equal,
FilterOperator::Contains,
FilterOperator::StartsWith,
FilterOperator::IsEmpty,
]),
'price' => Field::make('price')
->operators([
FilterOperator::Equal,
FilterOperator::GreaterThanOrEqual,
FilterOperator::LessThanOrEqual,
FilterOperator::Between,
])
->rules(['numeric', 'min:0'])
->searchable(false),
'category' => Field::related('category.name')->operators([FilterOperator::Equal]),
'tag' => Field::related('tags.name')->operators([FilterOperator::Equal, FilterOperator::In]),
];
}
/** Keep product results stable when the request omits sorting. */
protected function defaultSort(): array
{
return ['-id'];
}
}
use Alif\QueryFilter\Traits\Filterable;
use App\Filters\ProductFilter;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use Filterable;
protected $filterClass = ProductFilter::class;
}
// Uses the current HTTP query parameters and the model's filter class.
$products = Product::filter()->paginate(25);
// Explicit input works in HTTP handlers, jobs, commands, and tests.
$products = Product::filter(ProductFilter::class, [
'filter' => ['price' => ['between' => [10, 100]], 'tag' => 'featured'],
'sort' => '-price,id',
])->get();
$query = Product::query()->where('products.tenant_id', $tenantId);
(new ProductFilter($request->query()))->apply($query);
return $query->paginate(25);
$filter = new ProductFilter($request->query()); // Use ProductFilter's static defaults initially.
$filter->setWith(['category.parent', 'brand']); // Replace defaults for this instance only.
$relations = $filter->getWith(); // Read the configured relation array.
$products = Product::filter($filter)->paginate(25); // Eloquent loads the requested relations in batches.
use Illuminate\Database\Eloquent\Builder;
class DocumentFilter extends BaseEBFilter
{
/** Declare the document fields available to clients. */
protected function fields(): array
{
return ['title', 'status'];
}
}
$branchId = $request->user()->branch_id;
$filter = (new DocumentFilter($request->query()))->beforeUsing(
fn (Builder $query) => $query->where('documents.branch_id', $branchId),
);
return Document::filter($filter)->paginate(25);
use Alif\QueryFilter\Abstracts\BaseQBFilter;
use Illuminate\Support\Facades\DB;
class ProductQueryFilter extends BaseQBFilter
{
/** Map public product fields to the caller's joined SQL columns. */
protected function fields(): array
{
return ['name' => 'products.name', 'brand' => 'brands.name'];
}
}
$query = DB::table('products')
->leftJoin('brands', 'brands.id', '=', 'products.brand_id')
->select('products.*');
$filter = new ProductQueryFilter($request->query());
$filter->apply($query);
return $query->paginate(25);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.