承接 mehdirajabi/filterable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mehdirajabi/filterable

最新稳定版本:1.0.0

Composer 安装命令:

composer require mehdirajabi/filterable

包简介

Filter eloquent model by array query strings

README 文档

README

Introduction

The Laravel Filterable Trait is a powerful package providing a reusable trait for easily implementing filter functionality in your Laravel Eloquent models. It enables filtering query results based on multiple parameters, making it simpler to create dynamic and flexible APIs or data filtering mechanisms in your applications.

Features

  • Easily implement filtering on Eloquent models.
  • Filter data based on various query parameters.
  • Support for multiple filter types, such as exact matches, partial matches, ranges, etc.
  • Customizable and extensible for adding new filter types.
  • Improves code readability and maintainability by separating filtering logic from the main model.

Installation

You can install the package via Composer by running the following command:

composer require mehdirajabi/filterable

Usage

1. Apply the Filterable Trait to Your Eloquent Model

Open the Eloquent model where you want to apply the filtering functionality and add the Filterable trait:

use Mehdirajabi\Filterable\Filterable;

class User extends Model
{
    use Filterable;
}

In your controller:

class UserController extends Controller
{
    public function index()
    {
        return User::filter()->actived()->get();
    }
}

2. How Does Filterable Work?

Two query strings, filterColumns and filterValues, are used, both being arrays. The default operator for all columns is the "like" operator.

For example:

http://test.com/api/user/users?filterColumns[0]=name&filterValues[0]=Alex

3. Relational Filter

To filter on a relation, add the column name as nested. For example, if a user has a relation with a profile and the profile has a 'sex' column, use the following convention:

use Mehdirajabi\Filterable\Filterable;

class User extends Model
{
    use Filterable;

    public function profile()
    {
        return $this->hasOne(UserProfile::class);
    }
}
http://test.com/api/user/users?filterColumns[0]=profile.sex&filterValues[0]=male

4. Change Filter Mode Operator

To change the operator of the 'status' column, define the filterMode property:

use Mehdirajabi\Filterable\Operators\FilterEqual;

class User extends Model
{
    protected $filterMode = [
        'status' => FilterEqual::class
    ];
}

5. Change Operator in Controller

use Mehdirajabi\Filterable\Operators\FilterBetween;

class UserController extends Controller
{
    public function index()
    {
        return User::filter(['id' => FilterBetween::class])->paginate(100);
    }
}

Between Operator

use Mehdirajabi\Filterable\Operators\FilterBetween;

class User extends Model
{
    protected $filterMode = [
        'created_at' => FilterBetween::class
    ];
}
http://test.com/users?filterColumns[0]=created_at&filterValues[0]=2023-06-23 00:00:00, 2023-06-24 23:59:59

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-25