red-fern/laravel-array-query-builder
Composer 安装命令:
composer require red-fern/laravel-array-query-builder
包简介
Query an eloquent model using arrays
README 文档
README
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.
red-fern/laravel-array-query-builder 适用场景与选型建议
red-fern/laravel-array-query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 red-fern/laravel-array-query-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 red-fern/laravel-array-query-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-09