Skip to content

Commit

Permalink
[7.x] Cast limits to int (#39653)
Browse files Browse the repository at this point in the history
* wip

* [6.x] Fixes Query Builder to only cast integer when given other than `null` (#39644)

* Add failing tests

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Only cast integer when given other than null

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Improve tests

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
3 people committed Nov 17, 2021
1 parent cada3d2 commit fffe2c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Expand Up @@ -2049,7 +2049,7 @@ public function limit($value)
$property = $this->unions ? 'unionLimit' : 'limit';

if ($value >= 0) {
$this->$property = $value;
$this->$property = ! is_null($value) ? (int) $value : null;
}

return $this;
Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -1326,6 +1326,14 @@ public function testLimitsAndOffsets()
$builder->select('*')->from('users')->offset(5)->limit(10);
$this->assertSame('select * from "users" limit 10 offset 5', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->limit(null);
$this->assertSame('select * from "users"', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->limit(0);
$this->assertSame('select * from "users" limit 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->skip(5)->take(10);
$this->assertSame('select * from "users" limit 10 offset 5', $builder->toSql());
Expand All @@ -1337,6 +1345,14 @@ public function testLimitsAndOffsets()
$builder = $this->getBuilder();
$builder->select('*')->from('users')->skip(-5)->take(-10);
$this->assertSame('select * from "users" offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->skip(null)->take(null);
$this->assertSame('select * from "users" offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->skip(5)->take(null);
$this->assertSame('select * from "users" offset 5', $builder->toSql());
}

public function testForPage()
Expand Down

0 comments on commit fffe2c3

Please sign in to comment.