From fffe2c3a8b73aaeb3a560e39f1453b8dfe10eb08 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 17 Nov 2021 14:36:41 +0000 Subject: [PATCH] [7.x] Cast limits to int (#39653) * 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 * Only cast integer when given other than null Signed-off-by: Mior Muhammad Zaki * Improve tests Signed-off-by: Mior Muhammad Zaki Co-authored-by: Taylor Otwell Co-authored-by: Mior Muhammad Zaki --- src/Illuminate/Database/Query/Builder.php | 2 +- tests/Database/DatabaseQueryBuilderTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index adb8a8d5bc1e..c45eb6195287 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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; diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 7d53cb8f4a9c..69da93ba2417 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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()); @@ -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()