htetoozin / eloquent-filter
Laravel Eloquent Filter
v1.0.0
2023-12-22 08:35 UTC
README
📥 Installation
You can install this package via composer using this command:
composer require htetoozin/eloquent-filter
Usage
Run php artisan make:filter {name} this file generate and stored at the location of app/Filters/. eg..
php artisan make:filter UserFilter
will generate such file content of UserFilter.php
Important
Array value and the method name must be the same; otherwise, the filter will not work.
<?php namespace App\Filters; use HtetOoZin\EloquentFilter\Filters\Filter; class UserFilter extends Filter { /** * Register filter properties */ protected $filters = ['keyword']; /** * Filter by keyword. */ public function keyword($value) { return $this->builder->where('name', 'like', "%{$value}%"); } }
Add local query scope in related model.
<?php class User extends Model { public function scopeFilter($query, $filter) { $filter->apply($query); } }
Import UserFilter class to related controller.
<?php use App\Filters\UserFilter; class UserController extends Controller { public function index(UserFilter $filter) { return User::filter($filter)->get(); } }
📜 License
The MIT License (MIT). Please see License File for more information.