eth0/laravel-filters

A clean laravel model filters where we can filter laravel models by passing query string into it.

v1.1.0 2021-07-12 23:06 UTC

This package is auto-updated.

Last update: 2024-04-13 05:24:39 UTC


README

An clean way of filtering laravel models by query strings.

GitHub issues GitHub forks GitHub stars GitHub license Twitter

Installation

You can install this package via composer using this command:

composer require eth0/laravel-filters

The package will automatically register itself, but if your laravel versions is < 5.5 you will need to add Mukja\LaravelFilters\LaravelFiltersServiceProvider::class, service provider under your config/app.php file.

Documentation

Once the package is installed there will be 2 new artisan commands.

  • php artisan make:filter
  • php artisan make:model:filter

We can then generate a new model filter just by typing php artisan make:filter ModelFilter this will generate a new php file under app/Filters/ folder with the name ModelFilter.php which will look like below.

<?php

namespace App\Filters\ModelFilter;

use Mukja\LaravelFilters\FiltersAbstract;

class ModelFilter extends FiltersAbstract
{
    protected $filters = [
        //
    ];
}

On the $filters variable we can register all our model filters.

<?php

namespace App\Filters\ModelFilter;

use Mukja\LaravelFilters\FiltersAbstract;
use App\Filters\Model\{
    ModelStatusFilter,
    ModelCreatedAtFilter
};

class ModelFilter extends FiltersAbstract
{
    protected $filters = [
        'status' => ModelStatusFilter::class,
        'created_at' => ModelCreatedAtFilter::class
    ];
}

However we do not have created yet the model filters so lets create now the model filters.

Note: Pro tip if you add an prefix before your file like below Model a new folder Model will be generated inside your Filters folder so you can keep your filters tidy.

php artisan make:model:filter Model\ModelStatusFilter Model\ModelCreatedAtFilter

Below is one of the file generated:

<?php

namespace App\Filters\Model;

use Mukja\LaravelFilters\FilterAbstract;
use Illuminate\Database\Eloquent\Builder;

class ModelStatusFilter extends FilterAbstract
{
    /**
     * Define column mappings.
     * @return array
     */
    public function mappings ()
    {
        return [];
    }

    /**
     * Filter the column.
     * @param  Builder $builder
     * @param  mixed  $value
     * @return Builder $builder
     */
    public function filter (Builder $builder, $value)
    {
        //
    }
}

On the mappings method we can return an array of mappings for example if we do want to convert a few words to match our database columns like:

public function mappings ()
{
    return [
        'status' => 'active',
        's' => 'active'
    ];
}

Next on the filter method we can build our query filter like:

/**
 * Filter the column.
 * @param  Builder $builder
 * @param  mixed  $value
 * @return Builder $builder
 */
public function filter (Builder $builder, $value)
{
    return $builder->where('status', $value);
}

Once we have everything ready the last step is to add a scope to our laravel model which this filter will be used.

public function scopeFilter (Builder $builder, $request, array $filters = [])
{
    return (new ModelFilter($request))->filter($builder);
}

And then we can use our model filters everywhere on our applications just by adding the filter scope like:

$model = Model::filter($request);

Now we have our filters setup which mean we can now send to our server query string https:://example.com?status=active this will return us all the records where the status is active.

Road Map

Here's the plan for what's coming:

  • Add tests.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see feel free to make any pull request to make this package even better.

Security

If you discover any security related issues, please email e.mukja@icloud.com instead of using the issue tracker.

Credits

License

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