zaratesystems / laravel-filterable
Apply filters to your Eloquent models in Laravel
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 0
Type:package
Requires
- php: ^7.3
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^4.9|^5.8|^6.3
- phpunit/phpunit: ^9.0
- roave/security-advisories: dev-master
This package is not auto-updated.
Last update: 2024-11-20 15:54:25 UTC
README
Apply filters to your Eloquent models in Laravel.
Installation
You can install the package via composer:
composer require zaratesystems/laravel-filterable
The package will automatically register itself.
Usage
Add the Zarate\Filterable\Filterable
trait to your eloquent model.
Filterable add the filter scope to your Eloquent model.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Zarate\Filterable\Filterable; class User extends Model { use Filterable; }
Create a new filter class. this package add a new command for create filter class in the Filters directory.
php artisan make:filter UserFilter
Can see a new filter class in Filters Directory.
<?php namespace App\Filters; use Zarate\Filterable\QueryFilters; class UserFilter extends QueryFilters { /** * @param string $name * @return \Illuminate\Database\Eloquent\Builder */ public function name(string $name) { return $this->builder->where('name', $name); } }
Example for apply the filter class in a Controller.
<?php namespace App\Http\Controllers; use App\Filters\UserFilter; use App\Models\User; class UserController extends Controller { public function index(UserFilter $filters) { $users = User::filter($filters)->simplePaginate(); return view('users.index', compact('users')); } }
Example http request for filter users by name.
http://your-aplication.test/users?name=john
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.