buibr/eloquent-filter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

buibr/eloquent-filter

最新稳定版本:v1.2

Composer 安装命令:

composer require buibr/eloquent-filter

包简介

Search by filtering query string to eloquent query

README 文档

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.

统计信息

  • 总下载量: 295
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-09-14