Skip to content

Commit f5d6b37

Browse files
committed
Ensure JSON keys are in double quotes on updating JSON columns.
- Currently if your JSON keys have a dash, the update fails due to this issue https://bugs.mysql.com/bug.php?id=81896 - this change ensures the keys are within double quotes - before: “$.foo-bar” - after: '$."foo-bar"' - I changed the test to reflect the new format
1 parent 9285f28 commit f5d6b37

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/Illuminate/Database/Query/Grammars/MySqlGrammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected function compileJsonUpdateColumn($key, JsonExpression $value)
163163

164164
$field = $this->wrapValue(array_shift($path));
165165

166-
$accessor = '"$.'.implode('.', $path).'"';
166+
$accessor = "'$.\"".implode('.', $path)."\"'";
167167

168168
return "{$field} = json_set({$field}, {$accessor}, {$value->getValue()})";
169169
}

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ public function testMySqlUpdateWrappingJson()
16761676
$connection->expects($this->once())
16771677
->method('update')
16781678
->with(
1679-
'update `users` set `name` = json_set(`name`, "$.first_name", ?), `name` = json_set(`name`, "$.last_name", ?) where `active` = ?',
1679+
'update `users` set `name` = json_set(`name`, \'$."first_name"\', ?), `name` = json_set(`name`, \'$."last_name"\', ?) where `active` = ?',
16801680
['John', 'Doe', 1]
16811681
);
16821682

@@ -1694,7 +1694,7 @@ public function testMySqlUpdateWithJsonRemovesBindingsCorrectly()
16941694
$connection->shouldReceive('update')
16951695
->once()
16961696
->with(
1697-
'update `users` set `options` = json_set(`options`, "$.enable", false), `updated_at` = ? where `id` = ?',
1697+
'update `users` set `options` = json_set(`options`, \'$."enable"\', false), `updated_at` = ? where `id` = ?',
16981698
['2015-05-26 22:02:06', 0]
16991699
);
17001700
$builder = new Builder($connection, $grammar, $processor);

0 commit comments

Comments
 (0)