Skip to content

Commit 85fed0e

Browse files
JosephSilbertaylorotwell
authored andcommitted
Resolve paginators from the container (#19328)
1 parent f2e4859 commit 85fed0e

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/Illuminate/Database/Concerns/BuildsQueries.php

+38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Illuminate\Database\Concerns;
44

5+
use Illuminate\Container\Container;
6+
use Illuminate\Pagination\Paginator;
7+
use Illuminate\Pagination\LengthAwarePaginator;
8+
59
trait BuildsQueries
610
{
711
/**
@@ -89,4 +93,38 @@ public function when($value, $callback, $default = null)
8993

9094
return $this;
9195
}
96+
97+
/**
98+
* Create a new length-aware paginator instance.
99+
*
100+
* @param \Illuminate\Support\Collection $items
101+
* @param int $total
102+
* @param int $perPage
103+
* @param int $currentPage
104+
* @param array $options
105+
* @return \Illuminate\Pagination\LengthAwarePaginator
106+
*/
107+
protected function paginator($items, $total, $perPage, $currentPage, $options)
108+
{
109+
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
110+
'items', 'total', 'perPage', 'currentPage', 'options'
111+
));
112+
}
113+
114+
/**
115+
* Create a new simple paginator instance.
116+
*
117+
* @param \Illuminate\Support\Collection $items
118+
* @param int $total
119+
* @param int $perPage
120+
* @param int $currentPage
121+
* @param array $options
122+
* @return \Illuminate\Pagination\Paginator
123+
*/
124+
protected function simplePaginator($items, $perPage, $currentPage, $options)
125+
{
126+
return Container::getInstance()->makeWith(Paginator::class, compact(
127+
'items', 'perPage', 'currentPage', 'options'
128+
));
129+
}
92130
}

src/Illuminate/Database/Eloquent/Builder.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Illuminate\Pagination\Paginator;
1010
use Illuminate\Contracts\Support\Arrayable;
1111
use Illuminate\Database\Concerns\BuildsQueries;
12-
use Illuminate\Pagination\LengthAwarePaginator;
1312
use Illuminate\Database\Eloquent\Relations\Relation;
1413
use Illuminate\Database\Query\Builder as QueryBuilder;
1514

@@ -691,7 +690,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
691690
? $this->forPage($page, $perPage)->get($columns)
692691
: $this->model->newCollection();
693692

694-
return new LengthAwarePaginator($results, $total, $perPage, $page, [
693+
return $this->paginator($results, $total, $perPage, $page, [
695694
'path' => Paginator::resolveCurrentPath(),
696695
'pageName' => $pageName,
697696
]);
@@ -717,7 +716,7 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
717716
// paginator instances for these results with the given page and per page.
718717
$this->skip(($page - 1) * $perPage)->take($perPage + 1);
719718

720-
return new Paginator($this->get($columns), $perPage, $page, [
719+
return $this->simplePaginator($this->get($columns), $perPage, $page, [
721720
'path' => Paginator::resolveCurrentPath(),
722721
'pageName' => $pageName,
723722
]);

src/Illuminate/Database/Query/Builder.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Illuminate\Database\ConnectionInterface;
1616
use Illuminate\Database\Concerns\BuildsQueries;
1717
use Illuminate\Database\Query\Grammars\Grammar;
18-
use Illuminate\Pagination\LengthAwarePaginator;
1918
use Illuminate\Database\Query\Processors\Processor;
2019

2120
class Builder
@@ -1729,7 +1728,7 @@ public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $p
17291728

17301729
$results = $total ? $this->forPage($page, $perPage)->get($columns) : collect();
17311730

1732-
return new LengthAwarePaginator($results, $total, $perPage, $page, [
1731+
return $this->paginator($results, $total, $perPage, $page, [
17331732
'path' => Paginator::resolveCurrentPath(),
17341733
'pageName' => $pageName,
17351734
]);
@@ -1752,7 +1751,7 @@ public function simplePaginate($perPage = 15, $columns = ['*'], $pageName = 'pag
17521751

17531752
$this->skip(($page - 1) * $perPage)->take($perPage + 1);
17541753

1755-
return new Paginator($this->get($columns), $perPage, $page, [
1754+
return $this->simplePaginator($this->get($columns), $perPage, $page, [
17561755
'path' => Paginator::resolveCurrentPath(),
17571756
'pageName' => $pageName,
17581757
]);

0 commit comments

Comments
 (0)