Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix postgres grammar for nested json arrays #31448

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ protected function wrapJsonBooleanValue($value)
protected function wrapJsonPathAttributes($path)
{
return array_map(function ($attribute) {
if (\strval(\intval($attribute)) === $attribute) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use these val functions. Please use casts. They do the same.

return $attribute;
}

return "'$attribute'";
}, $path);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,10 @@ public function testPostgresWrappingJson()
$builder->select('*')->from('users')->where('items->price->in_usd', '=', 1)->where('items->age', '=', 2);
$this->assertSame('select * from "users" where "items"->\'price\'->>\'in_usd\' = ? and "items"->>\'age\' = ?', $builder->toSql());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('items->prices->0', '=', 1)->where('items->age', '=', 2);
$this->assertSame('select * from "users" where "items"->\'prices\'->>0 = ? and "items"->>\'age\' = ?', $builder->toSql());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('items->available', '=', true);
$this->assertSame('select * from "users" where ("items"->\'available\')::jsonb = \'true\'::jsonb', $builder->toSql());
Expand Down