laravelfilter maintained by jti
Description
Filter scope for Laravel
Author
Last update
2024/01/14 23:48
(dev-main)
License
Downloads
5
Tags
LaravelFilter
This is where your description should go. Take a look at contributing.md to see a to do list.
Installation
Via Composer
composer require jti/laravelfilter
Usage
- Use trait with scope in your Model
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Filterable;
}
trait Filterable
{
/**
* @param Builder $builder
* @param LaravelFilter $filter
*/
public function scopeFilter(Builder $builder, LaravelFilter $filter): void
{
$filter->apply($builder);
}
}
- Create your Filter class
class UserFilter extends \JTI\LaravelFilter\LaravelFilter
{
protected function initBuilder(\Illuminate\Database\Eloquent\Builder $builder): \Illuminate\Database\Eloquent\Builder
{
return $this->builder = $builder; // your model builder
}
// function name equal key name from array of params
public function email($email = '')
{
if ($email) {
$this->builder->where('email', '=', $name);
}
}
}
}
- Use filter in controller for example
class BuilderController extends Controller
{
public function index()
{
$filter = new UserFilter(['email' => 'example@gmail.com']);
$users = \App\Models\User::query()->filter($filter)->get();
return view('users', compact('users'));
}
}
Change log
Please see the changelog for more information on what has changed recently.
Testing
composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email yuriy.kernytskyi@jointoit.com instead of using the issue tracker.
Credits
License
MIT. Please see the license file for more information.