aidynmakhataev/eloquent-json-macros

用于 MySQL JSON 函数的 Laravel Eloquent Builder 宏

1.0.0 2018-07-29 16:20 UTC

This package is auto-updated.

Last update: 2024-09-29 05:08:26 UTC


README

此包可以帮助您以 Eloquent 风格和使用辅助函数的方式使用 MySQL JSON 函数。

Latest Stable Version Latest Unstable Version Total Downloads License

安装

您可以使用 composer 安装此包

$ composer require aidynmakhataev/eloquent-json-macros

特性

用法

假设我们有一个名为 events 的表,其中包含 json 列 - browsermembers;

浏览器(示例 json 对象)
{"os": "Windows", "name": "Safari", "resolution": {"x": 1920, "y": 1080}}  
成员(示例 json 数组)
[{"id": 6, "info": {"job": "Electrolytic Plating Machine Operator", "email": "prohaska.mervin@example.net", "card_type": "Visa"}, "name": "Prof. Eldridge Legros"}, {"id": 8, "info": {"job": "Urban Planner", "email": "casandra54@example.org", "card_type": "Master Card"}, "name": "Ms. Alayna Ziemann DDS"}]

jsonContains

向查询中添加 'JSON_CONTAINS' 条件以指示 JSON 文档是否在路径中包含特定对象

更多信息:https://dev.mysqlserver.cn/doc/refman/5.7/en/json-search-functions.html#function_json-contains

示例(针对 browserjson 对象列)
use App\Models\Event;

Event::jsonContains('browser->os', 'Windows')->get();
Event::jsonContains('browser->resolution.x', 1920)->get();
示例(针对 memberjson 数组列)
use App\Models\Event;

Event::jsonContains('members->[*].id', 6)->get();
Event::jsonContains('members->[1].info.email', 'casandra54@example.org')->get();

或orJsonContains

向查询中添加 orWhere 'JSON_CONTAINS' 条件以指示 JSON 文档是否在路径中包含特定对象

用法将与 jsonContains 宏相同;

whereJsonContainsPath

向查询中添加 'JSON_CONTAINS_PATH' 条件以指示 JSON 文档是否在路径中包含任何数据

更多信息:https://dev.mysqlserver.cn/doc/refman/5.7/en/json-search-functions.html#function_json-contains-path

示例(针对 browserjson 对象列)

针对单个路径

Event::whereJsonContainsPath('browser', 'resolution')->get();

针对多个路径

Event::whereJsonContainsPath('browser', ['resolution', 'os'])->get();

您还可以可选地传递第三个参数('one' 或 'all'),默认使用 'one';

Event::whereJsonContainsPath('browser', ['resolution', 'test'], 'all')->get();
示例(针对 memberjson 数组列)
Event::whereJsonContainsPath('members', '[*].info')->get();
Event::whereJsonContainsPath('members', ['[*].info', '[1].test'])->get();

orWhereJsonContainsPath

向查询中添加 orWhere 'JSON_CONTAINS_PATH' 条件以指示 JSON 文档是否在路径中包含任何数据

用法将与 whereJsonContainsPath 宏相同;

whereJsonDepth

向查询中添加 'JSON_DEPTH' 条件以指示 JSON 文档的深度

更多信息:https://dev.mysqlserver.cn/doc/refman/5.7/en/json-attribute-functions.html#function_json-depth

示例(针对 browserjson 对象列)
Event::whereJsonDepth('browser->resolution', '>', 1)->get();
Event::whereJsonDepth('browser->os',  2)->get();
Event::whereJsonDepth('browser->resolution.x',  '<=', 1)->get();
示例(针对 memberjson 数组列)
Event::whereJsonDepth('members->[*].info.job', '>=', 1)->get();
Event::whereJsonDepth('members->[1].info.phones[0].fax', '>', 0)->get();

orWhereJsonDepth

向查询中添加 orWhere 'JSON_DEPTH' 条件以指示 JSON 文档的深度

用法将与 whereJsonDepth 宏相同;

whereJsonExtract

向查询中添加 "JSON_EXTRACT" 条件。

更多信息:https://dev.mysqlserver.cn/doc/refman/5.7/en/json-search-functions.html#function_json-extract

示例(针对 browserjson 对象列)
Event::whereJsonExtract('browser->resolution.x', '>', 1500)->get();
Event::whereJsonExtract('browser->name', 'Mozilla Firefox')->get();
示例(针对 memberjson 数组列)
Event::whereJsonExtract('members->[0].id', '>=', 9)->get();
Event::whereJsonExtract('members->[*].info.job', 'LIKE', '%Cleaners%')->get();

orWhereJsonExtract

向查询中添加 orWhere "JSON_EXTRACT" 条件。

用法将与 whereJsonExtract 宏相同;

whereJsonLength

向查询中添加 'JSON_LENGTH' 条件。

更多信息:https://dev.mysqlserver.cn/doc/refman/5.7/en/json-attribute-functions.html#function_json-length

示例(针对 browserjson 对象列)
Event::whereJsonLength('browser->resolution', '>', 1)->get();
Event::whereJsonLength('browser->os',  4)->get();
Event::whereJsonLength('browser->resolution.x',  '>=', 1)->get();
示例(针对 memberjson 数组列)
Event::whereJsonLength('members->[*]', '>=', 1)->get();
Event::whereJsonLength('members->[1].info.phones[*].fax', '>', 0)->get();

orWhereJsonLength

向查询中添加 orWhere 'JSON_LENGTH' 条件

用法将与 whereJsonLength 宏相同;

TODO 其他宏和辅助函数的解释

贡献

  1. 分叉它 (https://github.com/AidynMakhataev/eloquent-json-macros/fork)
  2. 创建你的功能分支 (git checkout -b feature/fooBar)
  3. 提交你的更改 (git commit -am '添加一些fooBar')
  4. 推送到分支 (git push origin feature/fooBar)
  5. 创建新的Pull Request

安全性

如果你发现任何安全相关的问题,请通过电子邮件makataev.7@gmail.com联系,而不是使用问题跟踪器。

许可证

MIT