san4io / eloquent-filter
Laravel Eloquent Filter Package
Requires
- php: >=7.1
- illuminate/database: 5.*
- illuminate/support: 5.*
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^6.4
This package is not auto-updated.
Last update: 2024-05-12 00:48:09 UTC
README
Package to filter your Eloquent model simply and elegantly.
Requirements
- "php": ">=7.1",
- "illuminate/support": "5.*",
- "illuminate/database": "5.*"
Installation
composer require san4io/eloquent-filter
Usage
Model
Add Filterable trait to your eloquent model.
Create $filterable property with mappings corresponding to how model attributes should be filtered.
class Event extends Model { use Filterable; protected $fillable = [ 'country_id', 'title', ]; protected $filterable = [ 'country_id' => WhereFilter::class, 'title' => LikeFilter::class, 'created_at' => BetweenFilter::class ]; }
Controller
Just pass Request params to filter as array.
public function index(Request $request): JsonResponse { $events = Event::filter($request->all())->paginate(); // or get(), first() or whatever return response()->json($events); }
Request examples
http://localhost/api/v1/events?country_id=101&title=Sport
http://localhost/api/v1/events?country_id[]=101&country_id[]=102&title=Sport
http://localhost/api/v1/events?created_at[from]=2017-07-11&created_at[till]=2017-08-11&title=Sport
Filters
The package was build with mind that it should be easily extensible, that's why if you need some custom filters, you can extend AbstractFilter and add necessary mapping to the model.
By default where are 3 filter types:
- WhereFilter
- LikeFilter
- BetweenFilter
WhereFilter
Accepts integer or array of integers
LikeFilter
Accepts string
BetweenFilter
Accepts array with keys ['from'] and ['till']
Road map
- Tests
- Filtering Relations
Contribution
Any contributions welcome!