Downloads
Stars
Version
A Laravel package to filter Eloquent queries based on model properties.
You can install the package via Composer:
composer require sufian/laravel-query-filter
Add the Filterable trait to the Eloquent models you want to filter:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Sufian\QueryFilter\Filterable;
class User extends Model
{
use Filterable;
}
To generate a new filter class, use the make:filter Artisan command followed by the name of the class you want to create. For example, to create a filter class for the User model, run:
php artisan make:filter YourModel Name
Example:
php artisan make:filter User
OR Create filter classes in the App\Filters namespace for each model manually:
Example UserFilter Class
namespace App\Filters;
use Sufian\QueryFilter\BaseFilter;
use Illuminate\Database\Eloquent\Builder;
class UserFilter extends BaseFilter
{
protected $filters = ['name', 'email'];
public function name(Builder $query, $value)
{
$query->where('name', 'like', '%' . $value . '%');
}
public function email(Builder $query, $value)
{
$query->where('email', 'like', '%' . $value . '%');
}
}
You can now use the filter method in your controllers:
UserController
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$users = User::filter()->get();
return response()->json($users);
}
}
Thank you for considering contributing to the Laravel Query Filter package! Please read the contribution guidelines for details on how to contribute.
If you discover a security vulnerability within this Laravel Query Filter package, please send an e-mail to sufian at [email protected]. All security vulnerabilities will be promptly addressed.
This package is open-sourced software licensed under the MIT license.