Package Data | |
---|---|
Maintainer Username: | n7olkachev |
Maintainer Contact: | n7olkachev@gmail.com (Nikita Tolkachev) |
Package Create Date: | 2017-08-24 |
Package Last Update: | 2017-08-28 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-23 03:07:34 |
Package Statistics | |
---|---|
Total Downloads: | 89 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 34 |
Total Watchers: | 3 |
Total Forks: | 3 |
Total Open Issues: | 0 |
This package is powered by standard Laravel scopes, instead of other similar packages,
that brings something like Filter
classes to your code, so it is much more easy to jump into.
Also, if you decide to remove this package from your project, you will stay with standard scopes
which can be used directly further.
Personally, I use this trait for faster development, combining it with $request->all()
Page::filter($request->all())->get()
By default, you get equality filters (where field = bar
)
and when you need to support other queries, adding new scopes
will do the trick, without changing anything except model. See examples for better understanding.
class Page extends Model
{
use Filterable;
protected $fillable = [
'title',
'status',
'created_at',
];
protected $filterable = [
'title',
'created_after'
];
public function scopeCreatedAfter($query, $time)
{
return $query->where('created_at', '>', $time);
}
}
Now, we can use filter
scope to filter our queries:
Page::filter(['title' => 'Cool page'])->first(); // equals to where('title', 'Cool page')
Page::filter(['status' => ['new', 'active'])->get() // equals to whereIn('status', ['new', 'active'])
Page::filter(['created_after' => '2017-01-01'])->get() // equals to createdAfter('2017-01-01') (notice our scope in Page class)
Of course it supports filters with multiple keys:
Page::filter(['title' => 'Cool page', 'status' => 'active'])->first()
You can install the package via composer:
composer require n7olkachev/laravel-filterable
Next, add Filterable trait and list all filterable properties:
use Filterable;
protected $filterable = ['created_at', 'title'];
That's all!
$ composer test
https://websecret.by/
Web agency based in Minsk, Belarus
The MIT License (MIT)