scrnhq /
laravel-bakery
| 1 | <?php |
||
| 2 | |||
| 3 | use Illuminate\Pagination\Paginator; |
||
| 4 | use Illuminate\Database\Eloquent\Builder; |
||
| 5 | |||
| 6 | /* |
||
| 7 | * Paginate the given query. |
||
| 8 | * |
||
| 9 | * @param int $perPage |
||
| 10 | * @param array $columns |
||
| 11 | * @param string $pageName |
||
| 12 | * @param int|null $page |
||
| 13 | * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
||
| 14 | * |
||
| 15 | * @throws \InvalidArgumentException |
||
| 16 | */ |
||
| 17 | Builder::macro('bakeryPaginate', function ($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { |
||
| 18 | $page = $page ?: Paginator::resolveCurrentPage($pageName); |
||
| 19 | |||
| 20 | $perPage = $perPage ?: $this->model->getPerPage(); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 21 | |||
| 22 | $countColumns = $this->query->distinct ? [$this->model->getQualifiedKeyName()] : ['*']; |
||
| 23 | |||
| 24 | $results = ($total = $this->toBase()->getCountForPagination($countColumns)) |
||
| 25 | ? $this->forPage($page, $perPage)->get($columns) |
||
| 26 | : $this->model->newCollection(); |
||
| 27 | |||
| 28 | return $this->paginator($results, $total, $perPage, $page, [ |
||
| 29 | 'path' => Paginator::resolveCurrentPath(), |
||
| 30 | 'pageName' => $pageName, |
||
| 31 | ]); |
||
| 32 | }); |
||
| 33 |