定制 red-fern/laravel-array-query-builder 二次开发

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

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

red-fern/laravel-array-query-builder

Composer 安装命令:

composer require red-fern/laravel-array-query-builder

包简介

Query an eloquent model using arrays

README 文档

README

StyleCI

This package allows you to add where clauses to eloquent query builders using multidimensional arrays. By following a simple structure, you can chain multiple query conditions, nested conditions and relational queries.

$queryArray = [
    'condition' => 'or',
    'rules' => [
        [
            'field' => 'name',
            'operator' => '=',
            'value' => 'john'
        ],
        [
            'field' => 'age',
            'operator' => '>',
            'value' => 25
        ],
    ]
];

$users = User::arrayWheres($queryArray)->get();

The condition describes how the coinciding rules are applied. In this case the name and age fields will be wrapped in an OR query.

The above section would generate the following eloquent query builder:

$users = User::where('name', 'john')
            ->orWhere('age', '>', 25)
            ->get();

Operator Types

When querying against fields, there are several options for the operator. These options are:

Comparison Operators

The standard comparison operators e.g. =, <, >, <=, >=. An example below:

$rules = [
    'field' => 'age',
    'operator' => '>=',
    'value' => 25
];

This becomes:

$query->where('age','>=',25);

Nullable checks

Checking for if a column is "null" or "not null" e.g.

$rules = [
    'field' => 'dob',
    'operator' => 'null'
];

This becomes:

$query->whereNull('dob');

Between

Checking if a field value is between array of values:

$rules = [
    'field' => 'age',
    'operator' => 'between',
    'value' => [20, 50]
];

This becomes:

$query->whereBetween('age', [20, 50]);

In/Not in list of values

Checking if field value is "in" or "not in" an array of values:

$rules = [
    'field' => 'id',
    'operator' => 'in',
    'value' => [2, 5, 6]
];

This becomes:

$query->whereIn('id', [2, 5, 6]);

String comparison

Checking if a field value contains a string. This is basically an alias for a like query with wildcards e.g.

$rules = [
    'field' => 'name',
    'operator' => 'contains',
    'value' => 'john'
];

This becomes:

$query->where('name', 'like', '%john%');

Relations

You can also query eloquent relations using dot notation.

 $queryArray = [
    'condition' => 'and',
    'rules' => [
        [
            'field' => 'name',
            'operator' => 'like',
            'value' => '%john%'
        ],
        [
            'field' => 'orders.order_date',
            'operator' => '>',
            'value' => '2019-01-01 00:00:00'
        ]
    ]
];

$users = User::arrayWheres($queryArray)->get();

Using dot notation in the fields, will determine that it will query, in this case the orders relation. The above query would become:

$users = User::where('name', 'like', '%john%')
        ->whereHas('orders', function($q) {
            $q->where('order_date', '>', '2019-01-01 00:00:00');
        })->get();

Nested Queries

You can also add nested conditions to build more complex queries such as:

$rules = [
    'condition' => 'and',
    'rules' => [
        [
            'condition' => 'or',
            'rules' => [
                [
                    'field' => 'name',
                    'operator' => '=',
                    'value' => 'john'
                ],
                [
                    'field' => 'name',
                    'operator' => '=',
                    'value' => 'james'
                ],
            ]
        ],
        [
            'field' => 'orders.order_date',
            'operator' => '>',
            'value' => '2010-01-01 00:00:00'
        ],
    ]
];

$users = User::arrayWheres($queryArray)->get();

The above example shows how you can add nested conditional rules to the array. This follows the same format and you can nested to any depth. The above example would become:

$users = User::where(function($q) {
                $q->where('name', '=', 'john')
                    ->orWhere('name', '=', 'james');
            })
            ->whereHas('orders', function($q) {
                $q->where('order_date', '>', '2010-01-01 00:00:00');
            })->get();

Future Development

The plan is to expand the capabilities of this package, extending the array capability to also allow query selects, joins, order by, group bys etc.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固