blasttech /
laravel-paginate-plus
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Blasttech\PaginatePlus; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Builder; |
||||
| 6 | use Illuminate\Pagination\LengthAwarePaginator; |
||||
| 7 | use Illuminate\Support\Facades\DB; |
||||
| 8 | use Illuminate\Support\Facades\Request; |
||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * Trait PaginatePlusTrait. |
||||
| 12 | * |
||||
| 13 | * @method LengthAwarePaginator paginatePlus(int $perPage = null) |
||||
| 14 | */ |
||||
| 15 | trait PaginatePlusTrait |
||||
| 16 | { |
||||
| 17 | /** |
||||
| 18 | * @param static|$this|Builder $query |
||||
| 19 | * @param int|null $perPage |
||||
| 20 | * |
||||
| 21 | * @return LengthAwarePaginator |
||||
| 22 | */ |
||||
| 23 | public function scopePaginatePlus(Builder $query, $perPage = null) |
||||
| 24 | { |
||||
| 25 | $perPage = $perPage ?: $query->getModel()->getPerPage(); |
||||
| 26 | $currentPage = Request::input('page', 1); |
||||
| 27 | |||||
| 28 | // Total number of records in query |
||||
| 29 | $total = $this->getTotal($query); |
||||
| 30 | |||||
| 31 | // Items for current page |
||||
| 32 | $items = $this->getItems($query, $currentPage, $perPage); |
||||
| 33 | |||||
| 34 | $result = new LengthAwarePaginator( |
||||
| 35 | $items, |
||||
| 36 | $total, |
||||
| 37 | $perPage, |
||||
| 38 | $currentPage |
||||
| 39 | ); |
||||
| 40 | |||||
| 41 | return $result; |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * Get the total number of records in the query. |
||||
| 46 | * |
||||
| 47 | * @param Builder $query |
||||
| 48 | * |
||||
| 49 | * @return int |
||||
| 50 | */ |
||||
| 51 | protected function getTotal(Builder $query) |
||||
| 52 | { |
||||
| 53 | $clone = (clone $query); |
||||
| 54 | |||||
| 55 | return $clone |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 56 | ->getConnection() |
||||
| 57 | ->table(DB::raw("({$clone->toSql()}) as sub")) |
||||
| 58 | ->mergeBindings($clone->getQuery()) |
||||
|
0 ignored issues
–
show
The method
mergeBindings() does not exist on Illuminate\Database\ConnectionInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 59 | ->count(); |
||||
|
0 ignored issues
–
show
The method
count() does not exist on Illuminate\Database\ConnectionInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Get the items for the current page. |
||||
| 64 | * |
||||
| 65 | * @param Builder $query |
||||
| 66 | * @param int $currentPage |
||||
| 67 | * @param int $perPage |
||||
| 68 | * |
||||
| 69 | * @return array |
||||
| 70 | */ |
||||
| 71 | protected function getItems(Builder $query, $currentPage, $perPage) |
||||
| 72 | { |
||||
| 73 | $offSet = ($currentPage * $perPage) - $perPage; |
||||
| 74 | |||||
| 75 | return $query |
||||
| 76 | ->offset($offSet) |
||||
| 77 | ->limit($perPage) |
||||
| 78 | ->get() |
||||
| 79 | ->toArray(); |
||||
| 80 | } |
||||
| 81 | } |
||||
| 82 |