htetoozin/eloquent-filter is a Laravel package for laravel eloquent filter.
It currently has 0 GitHub stars and 7 downloads on Packagist (latest version v1.0.0).
Install it with composer require htetoozin/eloquent-filter.
Discover more Laravel packages by htetoozin
or browse all Laravel packages to compare alternatives.
Last updated
You can install this package via composer using this command:
composer require htetoozin/eloquent-filter
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();
}
}
The MIT License (MIT). Please see License File for more information.