buibr/eloquent-filter

There is no license information available for the latest version (v1.0) of this package.

Search by filtering query string to eloquent query

v1.0 2024-09-14 05:05 UTC

This package is auto-updated.

Last update: 2024-11-14 05:31:12 UTC


README

Latest Stable Version Total Downloads License

eloquent-filter is a simple package for filtering Eloquent queries based on request query parameters with predefined methods per parameter. It allows you to easily build complex query filters for your Laravel applications.

Installation

You can install the package via composer:

composer require buibr/eloquent-filter

Usage

Step 1: Add HasFilters Trait to Your Model

Include the HasFilters trait in any Eloquent model where you want to use query filters.

use BI\EloquentFilter\HasFilters;

class YourModel extends Model
{
    use HasFilters;

    // Other model code...
}

Step 2: Create a Filter Class

Create a new filter class that extends QueryFilter. In this class, define methods for each query parameter you want to filter by. Each method should accept the parameter value and modify the query accordingly.

use BI\EloquentFilter\QueryFilter;

class YourModelFilter extends QueryFilter
{
    public function status($value)
    {
        return $this->builder->where('status', $value);
    }

    public function category($value)
    {
        return $this->builder->where('category_id', $value);
    }

    // Add more filter methods as needed...
}

Step 3: Apply the Filter in Your Controller

In your controller, apply the filter to the query by using the filter scope. Pass an instance of your filter class as the argument.

use App\Models\YourModel;
use App\Filters\YourModelFilter;

class YourController extends Controller
{
    public function index(YourModelFilter $filters)
    {
        $items = YourModel::filter($filters)->get();

        return response()->json($items);
    }
}

Example Request

You can now filter the query by passing parameters in the request URL:

GET /your-models?status=active&category=1

This will apply the status and category filters based on the methods defined in your YourModelFilter class.

Customizing Filters

To customize how filters are applied, simply add more methods to your filter class. Each method corresponds to a query parameter and can modify the Eloquent query as needed.

class YourModelFilter extends QueryFilter
{
    public function created_at($value)
    {
        return $this->builder->whereDate('created_at', $value);
    }

    public function sort($value)
    {
        return $this->builder->orderBy('created_at', $value);
    }
}

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This package is open-sourced software licensed under the MIT license.