Skip to content

Commit

Permalink
增加whereJsonContains方法用于查询mysql的json数组数据
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Sep 4, 2024
1 parent a7c1324 commit 2dbbe9e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/db/concern/WhereQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '\')');
}

/**
* 比较两个字段.
*
Expand Down

0 comments on commit 2dbbe9e

Please sign in to comment.