croudtech/request-filters

Laravel request filtering helpers.

1.0.0 2021-05-10 20:39 UTC

This package is auto-updated.

Last update: 2024-11-11 04:24:17 UTC


README

This package is used to allow quick and dynamic filtering of eloquent based models using a simple request argument. Perfect for backend APIs!

Configuration

To begin using this package in Laravel, you should firstly create a macro for a request()->filter() method as shown below:

use CroudTech\RequestFilters\RequestFilters;

request()->macro('filters', function() {
    return RequestFilters::getFilterCollection(
        // Use the 'filters' query param.
        request()->query('filters')
    );
});

Note: For Lumen applications, you'll need a helper file to define the request() method as this is not available by default.

Now within any of your repositories, you can begin filtering the results from that model based on whatever strategy and parameter information was provided within the filters query parameter:

public function getFood()
{
    return request()->filters()->apply(
        DB::table('food')
    );
}

Now, we can provide a base_64 encoded string containing a JSON strategy for how we wish to filter our food. For example:

[
    {
        "strategy": "where",
        "parameters": [
            "calories",
            ">",
            100
        ]
    },
    {
        "strategy": "whereIn",
        "parameters": [
            "food_type",
            "('fried', 'baked')"
        ]
    }
]