Laravel 8 модельный фильтр
<?php
$filters = EloquentFilters::make([
new NameFilter($request->name)
]);
$products = Product::filter($filters)->get();
use Pricecurrent\LaravelEloquentFilters\AbstractEloquentFilter;
use Illuminate\Database\Eloquent\Builder;
class NameFilter extends AbstractEloquentFilter
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function apply(Builder $builder): Builder
{
return $query->where('name', 'like', "{$this->name}%");
}
}
$filters = EloquentFilters::make([
new NameFilter($request->user_name)
]);
$products = User::query()
->filter($filters)
->limit(10)
->latest()
->get();
Shadow