EloquentBuilder.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Base\Models;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Pagination\Paginator;
  5. /**
  6. * @mixin \Illuminate\Database\Query\Builder
  7. */
  8. class EloquentBuilder extends Builder
  9. {
  10. /**
  11. * Paginate the given query.
  12. *
  13. * @param int $perPage
  14. * @param array $columns
  15. * @param string $pageName
  16. * @param int|null $page
  17. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  18. *
  19. * @throws \InvalidArgumentException
  20. */
  21. public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
  22. {
  23. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  24. $perPage = $perPage ?: $this->model->getPerPage();
  25. $results = ($total = $this->toBase()->getCountForPagination($columns))
  26. ? $this->forPage($page, $perPage)->get($columns)
  27. : $this->model->newCollection();
  28. return $this->paginator($results, $total, $perPage, $page, [
  29. 'path' => Paginator::resolveCurrentPath(),
  30. 'pageName' => $pageName,
  31. ]);
  32. }
  33. }