Package to filter your Eloquent model simply and elegantly.
- "php": ">=7.1",
- "illuminate/support": "5.*",
- "illuminate/database": "5.*"
composer require san4io/eloquent-filter
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
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
Accepts integer or array of integers
Accepts string
Accepts array with keys ['from'] and ['till']
- Tests
- Filtering Relations
Any contributions welcome!