1. Go to this page and download the library: Download jerome/filterable 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/ */
jerome / filterable example snippets
namespace App\Filters;
use Filterable\Filter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;
class PostFilter extends Filter
{
/**
* Request keys that map straight to filter methods.
*
* Methods follow camelCased versions of the keys (e.g. published_after → publishedAfter).
*/
protected array $filters = ['status', 'published_after', 'q'];
public function __construct(Request $request)
{
parent::__construct($request);
$this->enableFeatures([
'validation',
'optimization',
'filterChaining',
'valueTransformation',
]);
$this->setValidationRules([
'status' => ['nullable', Rule::in(['draft', 'published'])],
'published_after' => ['nullable', 'date'],
]);
$this->registerTransformer('published_after', fn ($value) => Carbon::parse($value));
$this->registerPreFilters(fn (Builder $query) => $query->where('is_visible', true));
$this->select(['id', 'title', 'status', 'published_at'])->with('author');
}
protected function status(string $value): void
{
$this->getBuilder()->where('status', $value);
}
protected function publishedAfter(Carbon $date): void
{
$this->getBuilder()->whereDate('published_at', '>=', $date);
}
protected function q(string $term): void
{
$this->getBuilder()->where(function (Builder $query) use ($term) {
$query->where('title', 'like', "%{$term}%")
->orWhere('body', 'like', "%{$term}%");
});
}
}
namespace App\Models;
use Filterable\Traits\Filterable;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use Filterable;
}
namespace App\Http\Controllers;
use App\Filters\PostFilter;
use App\Http\Resources\PostResource;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController
{
public function index(Request $request, PostFilter $filter)
{
$posts = Post::query()
->filter(
$filter
->forUser($request->user())
->enableFeature('caching')
->setOptions(['chunk_size' => 500])
)
->get();
return PostResource::collection($posts);
}
}