This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
php artisan make:filter country
In the filter class which is created, implement your login in the apply()
method.
In order to get query value just use $this->value
. We retrieved it for you from your request.
...
public function apply(Builder $builder)
{
return $builder->where('country', $this->value);
}
...
Before registering you should use Filterable
trait in your model.
use Filterable;
...
...
protected static $filters = [
Country::class,
];
// All registered filtered are available through this method:
User::filter()->get();
// Only Specific registered filter is available and executable:
User::filterThrough([Country::class])->get();
You can install the package via composer:
composer require mjedari/larafilter
Then you can publish config file:
php artisan vendor:publish --provider "Mjedari\Larafilter\LarafilterServiceProvider"
Its simple.First create a filter by this command:
php artisan make:filter filter-name"
Command will create a class under the default directory App\Filters
:
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
use Mjedari\Larafilter\Filters\FilterContract;
class Active extends FilterContract
{
public function apply(Builder $builder)
{
// TODO: Implement apply() method.
}
public function options()
{
// TODO: Implement options() method.
}
/*
* Set rules on the query string
*/
public function rules()
{
return [
//
];
}
}
Your filter logic would be implemented in the apply()
method:
public function apply(Builder $builder)
{
return $builder->where('avtive', $this->value);
}
The important thing is that you have access query string value by $this->value
in your filter class.
For Which model you want to filter you should add Filterable
trait in it.
class User extends Authenticatable
{
use Filterable;
.
.
.
Then add related filters that you created. It should be static property:
use App\Filters\Active;
use App\Filters\City;
class User extends Authenticatable
{
use Filterable;
protected static $filters = [
Active::class,
City::class
];
.
.
.
Every thing is ready. just use it in your queries:
User::filter()->get();
if you want to specify some filter you can pass them thought this method:
User::filterThrough([City::class])->get();
It's good to mention that this package works with query string. Ex:
Route::get('/?country=germany', function() {
return User->filter()->get();
});
So you should pass params through the url. The default query name is filter class name. Of course you can change the filters query name by:
class CountryFilter extends FilterContract
{
public static $queryName = 'country';
.
.
.
Also, you can set rules on your query string parameters:
class Active extends FilterContract
{
public function rules()
{
return [
'required',
Rule::in(['true', 'false']),
];
}
More than that sometimes we would like cast query string value. So:
class Active extends FilterContract
{
protected $cast = 'boolean';
public function rules()
{
return [
//
];
}
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.