plutuss/query-filter-laravel

Query Filter Laravel package.

1.1.6 2024-02-03 12:42 UTC

This package is auto-updated.

Last update: 2024-10-22 11:32:49 UTC


README

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)
    {
        return $this->builder
            ->when($value, function ($query) use ($value) {
                return $query->where('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
    }
}