Skip to content

Commit d26b559

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 d26b559

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
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: 5 additions & 5 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);
@@ -1703,7 +1703,7 @@ public function testMySqlUpdateWithJsonRemovesBindingsCorrectly()
17031703
$connection->shouldReceive('update')
17041704
->once()
17051705
->with(
1706-
'update `users` set `options` = json_set(`options`, "$.size", 45), `updated_at` = ? where `id` = ?',
1706+
'update `users` set `options` = json_set(`options`, \'$."size"\', 45), `updated_at` = ? where `id` = ?',
17071707
['2015-05-26 22:02:06', 0]
17081708
);
17091709
$builder = new Builder($connection, $grammar, $processor);
@@ -1750,8 +1750,8 @@ public function testMySqlWrappingJsonWithBooleanAndIntegerThatLooksLikeOne()
17501750
public function testMySqlWrappingJson()
17511751
{
17521752
$builder = $this->getMySqlBuilder();
1753-
$builder->select('*')->from('users')->whereRaw('items->"$.price" = 1');
1754-
$this->assertEquals('select * from `users` where items->"$.price" = 1', $builder->toSql());
1753+
$builder->select('*')->from('users')->whereRaw('items->\'$."price"\' = 1');
1754+
$this->assertEquals('select * from `users` where items->\'$."price"\' = 1', $builder->toSql());
17551755

17561756
$builder = $this->getMySqlBuilder();
17571757
$builder->select('items->price')->from('users')->where('items->price', '=', 1)->orderBy('items->price');

0 commit comments

Comments
 (0)