Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,28 @@ public function newQuery()
return new static($this->connection, $this->grammar, $this->processor);
}

public function runPaginationCountQuery($columns = ['*'])
{
if ($this->distinct) {
throw new BadMethodCallException('Distinct queries cannot be used for pagination. Use GroupBy instead');
}

if ($this->groups || $this->havings) {
$without = $this->unions ? ['orders', 'limit', 'offset'] : ['columns', 'orders', 'limit', 'offset'];

$mql = $this->cloneWithout($without)
->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order'])
->toMql();

// Adds the $count stage to the pipeline
$mql['aggregate'][0][] = ['$count' => 'aggregate'];

return $this->collection->aggregate($mql['aggregate'][0], $mql['aggregate'][1])->toArray();
}

return parent::runPaginationCountQuery($columns);
}

/**
* Perform an update query.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MongoDB\Laravel\Tests;

use BadMethodCallException;
use DateTimeImmutable;
use LogicException;
use MongoDB\BSON\Regex;
Expand Down Expand Up @@ -534,6 +535,44 @@ public function testCursorPaginate(): void
$this->assertNull($results->first()->title);
}

public function testPaginateGroup(): void
{
// First page
$results = User::groupBy('age')->paginate(2);
$this->assertEquals(2, $results->count());
$this->assertEquals(6, $results->total());
$this->assertEquals(3, $results->lastPage());
$this->assertEquals(1, $results->currentPage());
$this->assertCount(2, $results->items());
$this->assertArrayHasKey('age', $results->first()->getAttributes());

// Last page has fewer results
$results = User::groupBy('age')->paginate(4, page: 2);
$this->assertEquals(2, $results->count());
$this->assertEquals(6, $results->total());
$this->assertEquals(2, $results->lastPage());
$this->assertEquals(2, $results->currentPage());
$this->assertCount(2, $results->items());
$this->assertArrayHasKey('age', $results->first()->getAttributes());

// Using a filter
$results = User::where('title', 'admin')->groupBy('age')->paginate(4);
$this->assertEquals(2, $results->count());
$this->assertEquals(2, $results->total());
$this->assertEquals(1, $results->lastPage());
$this->assertEquals(1, $results->currentPage());
$this->assertCount(2, $results->items());
$this->assertArrayHasKey('age', $results->last()->getAttributes());
}

public function testPaginateDistinct(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Distinct queries cannot be used for pagination. Use GroupBy instead');

User::distinct('age')->paginate(2);
}

public function testUpdate(): void
{
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));
Expand Down