In Laravel, we commonly face the problem of adding repetitive filtering code, sorting and search as well this package will address this problem.
composer require kalimeromk/filterable
Use Filterable;
$getFillable: Specify all the fields which exist in your table.
$boolFields:- Add fields on which you want to apply Boolean filtering.
protected array $getFillable = [
'name',
'email',
'address',
];
protected array $boolFields = [
'is_active',
];
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
protected $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function index(Request $request)
{
$users = $this->model
->filter($request->all())
->get();
return view('users.index', compact('users'));
}
}
Use Sortable;
$sortable:- Add fields on which you want to apply sort property.
protected array $sortable = [
'id',
'name',
'email',
'address'
];
Example below allows sorting for the columns: id, name, email, address
<?php
namespace App;
use Kalimeromk\Filterable\Trait\Sortable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Sortable;
public $sortables = ['id', 'name', 'email', 'address'];
}
Below is an example of the usage of the sortable trait (query scope).
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
protected $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function index(Request $request)
{
$users = $this->model
->sort($request->all())
->get();
return view('users.index', compact('users'));
}
}
$likeRows:- Add fields on which you want to apply whereLike property.
public const likeRows = [
'name',
'email',
'address'
];
Example below allows searching for the columns: name, email, address
<?php
namespace App;
use Kalimeromk\Filterable\Trait\Sortable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public const likeRows = [
'name',
'email',
'address'
];
}
this method can be used to search try relation as well just update const with relation and row name for search
public const likeRows = [
'name',
'email',
'address',
'country.name'
];
Below is an example of the usage of the whereLike method (query scope).
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
protected $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function index(Request $request)
{
$users = $this->model
->whereLike(User::likeRows, Arr::get($request, 'search'))
->get();
return view('users.index', compact('users'));
}
}
NOTE: This also works with filter, sort and with pagination
$users = $this->model->whereLike(User::likeRows, Arr::get($request, 'search'))->sort($request->all())->filter($request->all())->paginate(10)
Run the tests with:
vendor/bin/phpunit
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email [email protected] or use the issue tracker.
The MIT License (MIT). Please see License File for more information.