Skip to content

This package is a simple object-oriented query string filter for Laravel applications. you can create a filter class -by command and make your query filtering logic clean and strongly maintainable.

License

Notifications You must be signed in to change notification settings

mjedari/larafilter

Repository files navigation

Laravel Query String Filter

Latest Version on Packagist Build Status Scrutinizer Code Quality Quality Score PHP Tests PHP 8 Tests Total Downloads

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.

Quick start

Create a filter

php artisan make:filter country

Implement your filter logic

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);
}

...

Register filter class for model

Before registering you should use Filterable trait in your model.

use Filterable;
...

...

protected static $filters = [
    Country::class,
];

Use it!

// All registered filtered are available through this method:

User::filter()->get();


// Only Specific registered filter is available and executable:

User::filterThrough([Country::class])->get();

Installation

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"

Usage

Initiation

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.

Using

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 [
            //
        ];
    }

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

This package is a simple object-oriented query string filter for Laravel applications. you can create a filter class -by command and make your query filtering logic clean and strongly maintainable.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages