1. Go to this page and download the library: Download culturegr/filterer 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/ */
culturegr / filterer example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
protected $date = ['registered_at'];
public function country()
{
return $this->belongsTo(Country::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
public function favoriteProducts()
{
return $this->belongsToMany(FavoriteProduct::class);
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use CultureGr\Filterer\Filterable;
class Client extends Model
{
use Filterable;
...
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use CultureGr\Filterer\Filterable;
class Client extends Model
{
use Filterable;
protected $filterable = ['age', 'country.name', 'orders.items', 'favoriteProducts.price'];
protected $sortable = ['age', 'country.name', 'orders.items', 'favoriteProducts.price'];
...
}
namespace App\CustomFilters;
use CultureGr\Filterer\Contracts\CustomFilterInterface;
use Illuminate\Database\Eloquent\Builder;
class ActiveClientsFilter implements CustomFilterInterface
{
/**
* Apply the custom filter to the query builder.
*
* @param Builder $builder The query builder instance
* @param array<string, mixed> $filter The filter array
*
* @phpstan-param array{
* column: string,
* operator: string,
* query_1: string,
* query_2: string
* } $filter
*/
public function apply(Builder $builder, $filter): Builder
{
// Your custom filter logic here...
$queryValue = $filter['query_1']
if ($queryValue === '1') {
$builder->whereHas('orders', function($q) {
$q->where('created_at', '>=', Carbon::now()->subDays(30));
});
} else {
$builder->where(function($query) {
$query->whereDoesntHave('orders')
->orWhereHas('orders', function($q) {
$q->where('created_at', '<', Carbon::now()->subDays(30));
});
});
}
}
}
use CultureGr\Filterer\Filterable;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
use Filterable;
//...
protected $customFilters = [
'active' => ActiveClientsFilter::class,
];
//...
}
// ❌ Missing page in stored data
$stored = ['limit' => 20, 'filters' => [...]];
// ✅ Include page in stored data
$stored = ['page' => 2, 'limit' => 20, 'filters' => [...]];
// Simple pagination (no total count, more efficient)
$clients = Client::filterSimplePaginate([
'filters' => [...],
'sorts' => [...],
'limit' => 20,
'page' => 1,
]);
// Returns Illuminate\Pagination\Paginator (simple paginator)
// Get count of filtered results
$count = Client::filterCount([
'filters' => [
[
'column' => 'status',
'operator' => 'equal_to',
'query_1' => 'active',
],
],
// Note: sorts are ignored for counting (performance optimization)
]);
// Returns integer count
'pagination' => [
'default_limit' => env('FILTERER_DEFAULT_LIMIT', 10), // Default items per page
'max_limit' => env('FILTERER_MAX_LIMIT'), // Maximum items per page (null = no limit)
'page_name' => env('FILTERER_PAGE_NAME', 'page'), // Query parameter name for pagination
],
'validation' => [
'error_messages' => [ // Custom validation error messages (empty = use app translations)
'limit.max' => 'The limit may not be greater than :max.',
'filters.*.column.in' => 'The selected filter column is not allowed.',
// ... other custom messages
],
],
'security' => [
'allowed_operators' => [ // Restrict available operators globally
'equal_to',
'contains',
// ... only allow specific operators (empty = allow all)
],
],