Skip to content

Commit

Permalink
Resolve paginators from the container (#19328)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber authored and taylorotwell committed May 24, 2017
1 parent f2e4859 commit 85fed0e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
38 changes: 38 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Illuminate\Database\Concerns;

use Illuminate\Container\Container;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

trait BuildsQueries
{
/**
Expand Down Expand Up @@ -89,4 +93,38 @@ public function when($value, $callback, $default = null)

return $this;
}

/**
* Create a new length-aware paginator instance.
*
* @param \Illuminate\Support\Collection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
protected function paginator($items, $total, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}

/**
* Create a new simple paginator instance.
*
* @param \Illuminate\Support\Collection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return \Illuminate\Pagination\Paginator
*/
protected function simplePaginator($items, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(Paginator::class, compact(
'items', 'perPage', 'currentPage', 'options'
));
}
}
5 changes: 2 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Pagination\Paginator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Concerns\BuildsQueries;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;

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

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

return new Paginator($this->get($columns), $perPage, $page, [
return $this->simplePaginator($this->get($columns), $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
Expand Down
5 changes: 2 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Concerns\BuildsQueries;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Database\Query\Processors\Processor;

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

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

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

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

return new Paginator($this->get($columns), $perPage, $page, [
return $this->simplePaginator($this->get($columns), $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
Expand Down

1 comment on commit 85fed0e

@lukepolo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, i was going todo this in my other PR but figured it would have gotten rejected!

Please sign in to comment.