1. Go to this page and download the library: Download davidoc26/eloquent-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/ */
davidoc26 / eloquent-filter example snippets
use Davidoc26\EloquentFilter\Traits\Filterable;
class Post extends Model
{
use Filterable;
}
public function getFilters(): array
{
return [
FirstFilter::class,
SecondFilter::class => ['argument' => 20], // As mentioned above, filters can have their own arguments.
];
}
artisan make:filter MyFilter
use Davidoc26\EloquentFilter\Filters\Filter;
class MyFilter extends Filter
{
public function filter(Builder $builder, Closure $next): Builder
{
//
return $next($builder);
}
}
artisan make:request-filter MyRequestFilter
use Davidoc26\EloquentFilter\Filters\RequestFilter;
class MyRequestFilter extends RequestFilter
{
public function filter(Builder $builder, Closure $next): Builder
{
// $this->request
return $next($builder);
}
}
public function getFilters(): array
{
return [
LimitFilter::class => ['limit' => 10], // For convenience, specify the arguments in an array.
];
}
use Davidoc26\EloquentFilter\Filters\RequestFilter;
use Davidoc26\EloquentFilter\Traits\HasArguments;
class LimitFilter extends RequestFilter
{
use HasArguments;
public function filter(Builder $builder, Closure $next): Builder
{
// If there is no limit in the request, we use the limit specified in the model.
$builder->when(
$this->request->input('limit', $this->limit),
fn(Builder $builder, $limit) => $builder->limit($limit)
);
// Note that you must always return this.
return $next($builder);
}
}
artisan make:filter-pack MyFilterPack
namespace App\Filters\Packs;
use Davidoc26\EloquentFilter\Packs\FilterPack;
class MyFilterPack extends FilterPack
{
public function getFilters(): array
{
return [
// Define your filters here.
];
}
}
Post::withFilterPacks([MyFilterPack::class])->get();
// The meaning of the filter pack is that you can use it on any model that uses the Filterable trait
User::withFilterPacks([
MyFilterPack::class,
SecondFilterPack::class,
])->get();
Post::filter()->get();
// You can also pass additional filters:
Post::filter([OrderByFilter::class])->get();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.