laraditz / saring
A simple eloquent model filter.
Installs: 135
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:laravel-package
Requires
- php: ^7.2.5|^8.0
- illuminate/database: ^6.0|^7.0|^8.40
- illuminate/http: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2024-12-19 16:00:35 UTC
README
A simple eloquent model filter for Laravel and Lumen.
Installation
Via Composer
$ composer require laraditz/saring
Configuration
Add filterable trait to your model as below snippet:
use Laraditz\Saring\Filterable; class User extends Model implements AuthenticatableContract, AuthorizableContract { use Filterable; ... }
Create filter class under the App/Filters
folder with <model_name>Filter
format. For example for User
model, you will need to create UserFilter
class.
Below snippet shows how the UserFilter
could look like:
namespace App\Filters; use Laraditz\Saring\Filter; class UserFilter extends Filter { public function name($value) { $this->where('name', 'LIKE', $value); } public function email($value) { $this->where('email', 'LIKE', "%$value%"); } }
If you want to have more control on which attributes can be filtered, you can add filterable
array to you model:
protected $filterable = [ 'name', 'email' ];
Usage
In your controller, call filter
method and pass the input data to use the filter that you have created.
$users = User::filter($request->all())->get();
That's it!
Credits
License
MIT. Please see the license file for more information.