PageTrait.php 690 B

123456789101112131415161718192021
  1. <?php
  2. namespace App\Traits;
  3. use App\Traits\PageDefaultInterface;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Pagination\Paginator;
  6. trait PageTrait
  7. {
  8. public static function JsonPage(Builder $builder, $pageSize = PageDefaultInterface::PAGE_SIZE, $pageName = PageDefaultInterface::PAGE_NAME)
  9. {
  10. $page = Paginator::resolveCurrentPage($pageName);
  11. $offset = $page == 1 ? 0 : ($page-1) * $pageSize;
  12. return [
  13. 'count' => $builder->count(),
  14. 'list' => $builder->offset($offset)->limit($pageSize)->get()->toArray(),
  15. 'current_page' => $page,
  16. 'page_size' => $pageSize
  17. ];
  18. }
  19. }