Skip to content

Commit 5e0b273

Browse files
authored
use jsonb type for pgsql whereJsonLength (#39619)
1 parent 2534e00 commit 5e0b273

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function compileJsonLength($column, $operator, $value)
138138
{
139139
$column = str_replace('->>', '->', $this->wrap($column));
140140

141-
return 'json_array_length(('.$column.')::json) '.$operator.' '.$value;
141+
return 'jsonb_array_length(('.$column.')::jsonb) '.$operator.' '.$value;
142142
}
143143

144144
/**

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4170,22 +4170,22 @@ public function testWhereJsonLengthPostgres()
41704170
{
41714171
$builder = $this->getPostgresBuilder();
41724172
$builder->select('*')->from('users')->whereJsonLength('options', 0);
4173-
$this->assertSame('select * from "users" where json_array_length(("options")::json) = ?', $builder->toSql());
4173+
$this->assertSame('select * from "users" where jsonb_array_length(("options")::jsonb) = ?', $builder->toSql());
41744174
$this->assertEquals([0], $builder->getBindings());
41754175

41764176
$builder = $this->getPostgresBuilder();
41774177
$builder->select('*')->from('users')->whereJsonLength('users.options->languages', '>', 0);
4178-
$this->assertSame('select * from "users" where json_array_length(("users"."options"->\'languages\')::json) > ?', $builder->toSql());
4178+
$this->assertSame('select * from "users" where jsonb_array_length(("users"."options"->\'languages\')::jsonb) > ?', $builder->toSql());
41794179
$this->assertEquals([0], $builder->getBindings());
41804180

41814181
$builder = $this->getPostgresBuilder();
41824182
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', new Raw('0'));
4183-
$this->assertSame('select * from "users" where "id" = ? or json_array_length(("options"->\'languages\')::json) = 0', $builder->toSql());
4183+
$this->assertSame('select * from "users" where "id" = ? or jsonb_array_length(("options"->\'languages\')::jsonb) = 0', $builder->toSql());
41844184
$this->assertEquals([1], $builder->getBindings());
41854185

41864186
$builder = $this->getPostgresBuilder();
41874187
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', '>', new Raw('0'));
4188-
$this->assertSame('select * from "users" where "id" = ? or json_array_length(("options"->\'languages\')::json) > 0', $builder->toSql());
4188+
$this->assertSame('select * from "users" where "id" = ? or jsonb_array_length(("options"->\'languages\')::jsonb) > 0', $builder->toSql());
41894189
$this->assertEquals([1], $builder->getBindings());
41904190
}
41914191

0 commit comments

Comments
 (0)