Laravel:
composer require plutuss/query-filter-laravel
php artisan make:query-filter NameFilter
<?php
declare(strict_types=1);
namespace App\Filters;
use Plutuss\Filter\QueryFilter;
class UserFilter extends QueryFilter
{
public function name($value) // public function first_name($value) or public function firstName($value)
{
return $this->builder
->when($value, function ($query) use ($value) {
return $query->where('name', 'like', '%' . $value . '%');
// return $query->where('first_name', 'like', '%' . $value . '%');
});
}
public function email($value)
{
return $this->builder
->when($value, function ($query) use ($value) {
return $query->where('email', $value);
});
}
}
- App\Models
Plutuss\Traits\HasQueryFilter
<?php
namespace App\Models;
use Plutuss\Traits\HasQueryFilter;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasQueryFilter;
- Controller
<?php
namespace App\Http\Controllers;
use App\Filters\UserFilter;
use App\Models\User;
class UserController extends Controller
{
public function index(UserFilter $userFilter)
{
$users = User::query()->filter($userFilter)->get();
return $users
}
}