定制 kayckmatias/z-filters 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kayckmatias/z-filters

最新稳定版本:0.0.1

Composer 安装命令:

composer require kayckmatias/z-filters

包简介

a simple way to dynamically filter your Laravel models

README 文档

README

a simple package to filter your Laravel models without repetitive loops or various if-else

Version Total Downloads License

Installation

You can install the package via composer:

composer require kayckmatias/z-filters

Usage

You just need two things: make a new filter and create a scope in your model to point filter.

To make a new Filter:

php artisan make:zfilter NameFilter

Example:

Example new Filter Image

and configure your filter options according to your preference (read here)

Now, is need make a scope in your model, example:

public function scopeFilterBy(Builder $query, array $filters)
{
    $filter = new UsersFilter($query, $filters);

    return $filter->apply();
}

Configure

zFilters supports three filters type: simple, relation and complex.

Simple Filters:

Simple Filter is a whereIn condition, when you make a simple filter you is saying.

'custom_name' => 'column to verify set whereIn'

Let's assume that your user table has the column "department_id", to make a simple filter you just need to reference how the name of the filter option will be, let's call it "departments" and the column, which would be "department_id"

'departaments' => 'departament_id'

Now the array of values or single value you send in $filters['departments'] will be filtered in whereIn condition on the simple filter

Relation Filters:

Relation Filter is a whereIn condition in a relation, when you make a relation filter you is saying.

'custom_name' => ['relation' => 'column to verify set whereIn']

Let's assume that your User model has the relation with department (departments()), to create a relationship filter where we must get all the users that the department belongs to manager 1 or 2 we can do this:

'departments_manager' => ['departments' => 'manager_id']

Now the array of values or single value you send in $filters['departments_manager'] will be filtered in whereIn condition in departments relation on manager_id column.

Complex Filters

The complex filter can deal with more specific conditions, it accepts a callback function and you can filter however you want, let's go to another example. Assuming your same user table has a name column and a summary column and you expect to do a complex search for both conditions by value, one way to do it would be:

'search' => function ($q, $filterValue) {
    $q->where(function ($q) use ($filterValue) {
        $q->where('name', 'LIKE', "%" . $filterValue . "%");
        $q->orWhere('summary', 'LIKE', "%" . $filterValue . "%");
    });
}

The complex filter option named 'search' will execute the function, the value of $q will be the query builder being formed and the second parameter, $filterValue will be automatically implemented to what is sent in $filters['search'], as it is a callback function you can work on it too, manipulate the values sent, as if it were inside the query builder itself.

License

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


Made with ♥ by Kayck Matias

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固