Skip to content

Commit 79583dc

Browse files
committed
优化whereJsonContains方法
1 parent dd83ca5 commit 79583dc

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

src/db/concern/WhereQuery.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,13 @@ public function whereFindInSet(string $field, $condition, string $logic = 'AND')
304304
*/
305305
public function whereJsonContains(string $field, $condition, string $logic = 'AND')
306306
{
307-
if (str_contains($field, '->')) {
308-
[$field1, $field2] = explode('->', $field);
309-
$field = 'json_extract(' . $field1 . ',\'$.' . $field2 . '\')';
310-
}
311-
312307
$value = is_null($condition) ? 'NULL' : '\'' . json_encode($condition) . '\'';
313308

314-
return $this->whereRaw('json_contains(' . $field . ',' . $value . ')', [], $logic);
309+
if (str_contains($field, '->')) {
310+
[$field, $path] = explode('->', $field, 2);
311+
return $this->whereRaw('json_contains(' . $field . ', ' . $value . ', \'$.'. str_replace('->', '.', $path) . '\')', [], $logic);
312+
}
313+
return $this->whereRaw('json_contains(' . $field . ', ' . $value . ')', [], $logic);
315314
}
316315

317316
public function whereOrJsonContains(string $field, $condition)

0 commit comments

Comments
 (0)