Skip to content

Commit

Permalink
Merge pull request #54 from hungthai1401/feature/query_in_not_exists_…
Browse files Browse the repository at this point in the history
…page_prevented

Prevent query when the keys are empty
  • Loading branch information
aarondfrancis committed May 15, 2024
2 parents 1fac167 + 048c6c4 commit 273cda1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/FastPaginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ protected function paginate(string $paginationMethod, Closure $paginatorOutput)
// Get the key values from the records on the current page without mutating them.
$ids = $paginator->getCollection()->map->getRawOriginal($key)->toArray();

if (count($ids) <= 0) {
return $paginator;
}

if (in_array($model->getKeyType(), ['int', 'integer'])) {
$this->query->whereIntegerInRaw("$table.$key", $ids);
} else {
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ public function custom_page_is_preserved()
$this->assertEquals(self::TOTAL_USERS, $results->total());
}

/** @test */
public function not_exists_page_is_preserved()
{
$exists = User::query()->fastPaginate();

$queries = $this->withQueriesLogged(function () use (&$doesnt) {
$doesnt = User::query()->fastPaginate(2, ['*'], 'page', 16);
});

$this->assertEquals(get_class($exists), get_class($doesnt));

/** @var \Illuminate\Pagination\LengthAwarePaginator $doesnt */
$this->assertEquals(0, $doesnt->count());
$this->assertArrayNotHasKey(2, $queries);

$this->assertFalse($doesnt->hasMorePages());
}

/** @test */
public function custom_table_is_preserved()
{
Expand Down

0 comments on commit 273cda1

Please sign in to comment.