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,
];
//...
}