From 2dbbe9ecabf02316e2c9584420cc1205742ad734 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 4 Sep 2024 11:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0whereJsonContains=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=94=A8=E4=BA=8E=E6=9F=A5=E8=AF=A2mysql=E7=9A=84json?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/concern/WhereQuery.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/db/concern/WhereQuery.php b/src/db/concern/WhereQuery.php index 6a6741bc..6d2c8644 100644 --- a/src/db/concern/WhereQuery.php +++ b/src/db/concern/WhereQuery.php @@ -273,6 +273,26 @@ public function whereFindInSet(string $field, $condition, string $logic = 'AND') return $this->parseWhereExp($logic, $field, 'FIND IN SET', $condition, [], true); } + /** + * 指定json_contains查询条件. + * + * @param mixed $field 查询字段 + * @param mixed $condition 查询条件 + * @param string $logic 查询逻辑 and or xor + * + * @return $this + */ + public function whereJsonContains(string $field, $condition, string $logic = 'AND') + { + if (str_contains($field, '->')) { + [$field1, $field2] = explode('->', $field); + $field = 'json_extract(' . $field1 . ',\'$.' . $field2 . '\')'; + } + + $value = is_string($condition) ? '"' . $condition . '"' : $condition; + return $this->whereRaw('json_contains(' . $field . ',\'' . $value . '\')'); + } + /** * 比较两个字段. *