BaseRepository.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Repositories\Eloquent;
  11. use App\Repositories\Presenters\Presenter;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Prettus\Repository\Eloquent\BaseRepository as BaseRepositoryEloquent;
  14. abstract class BaseRepository extends BaseRepositoryEloquent
  15. {
  16. /**
  17. * @var Presenter
  18. */
  19. protected $presenter;
  20. /**
  21. * Retrieve all data of repository, simple paginated.
  22. *
  23. * @param null|int $limit
  24. * @param array $columns
  25. * @return mixed
  26. */
  27. public function cursorPaginate($limit = null, $columns = ['*'])
  28. {
  29. $this->applyCriteria();
  30. $this->applyScope();
  31. $limit = is_null($limit) ? config('repository.pagination.limit', 15) : (int)$limit;
  32. $results = $this->model->select($columns)->limit($limit)->get();
  33. if ($this->model instanceof Builder) {
  34. $primaryKey = $this->model->getModel()->getKeyName();
  35. } else {
  36. $primaryKey = $this->model->getKeyName();
  37. }
  38. $count = $results->count();
  39. $next = $count === $limit ? optional($results->last())->{$primaryKey} : null;
  40. $prev = request('prev');
  41. $this->presenter->makeCursor((int)request('cursor'), $prev ? (int)$prev : '', $next, $count);
  42. $this->resetModel();
  43. return $this->parserResult($results);
  44. }
  45. }