Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66ab8a7

Browse files
committed
Merge pull request laravel#3017 from KaneCohen/db-offset
Fix DB offset method. Can not be negative.
2 parents 8614960 + 79b6979 commit 66ab8a7

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/Illuminate/Database/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ public function orderByRaw($sql, $bindings = array())
954954
*/
955955
public function offset($value)
956956
{
957-
$this->offset = $value;
957+
$this->offset = max(0, $value);
958958

959959
return $this;
960960
}

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,17 @@ public function testLimitsAndOffsets()
347347
$builder->select('*')->from('users')->skip(5)->take(10);
348348
$this->assertEquals('select * from "users" limit 10 offset 5', $builder->toSql());
349349

350+
$builder = $this->getBuilder();
351+
$builder->select('*')->from('users')->skip(-5)->take(10);
352+
$this->assertEquals('select * from "users" limit 10 offset 0', $builder->toSql());
353+
350354
$builder = $this->getBuilder();
351355
$builder->select('*')->from('users')->forPage(2, 15);
352356
$this->assertEquals('select * from "users" limit 15 offset 15', $builder->toSql());
357+
358+
$builder = $this->getBuilder();
359+
$builder->select('*')->from('users')->forPage(-2, 15);
360+
$this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql());
353361
}
354362

355363

0 commit comments

Comments
 (0)