Lars-Janssen's avatar

Repository pattern pagination

Hi,

Is it fine to return ->paginate(5); (Illuminate\Pagination\LengthAwarePaginator) within a eloquent repository?

Right now I've this in my repository:

/**
 * Get Results by Page.
 *
 * @param int $page
 * @param int $limit
 * @param array $with
 * @return StdClass Object with $items and $totalItems for pagination
 */
public function paginate($page = 1, $limit = 10, $with = [])
{
    $result = new StdClass;
    $result->page = $page;
    $result->limit = $limit;
    $result->totalItems = 0;
    $result->items = [];

    $query = $this->make($with);

    $model = $query
        ->skip($limit * ($page - 1))
        ->take($limit)
        ->get();

    $result->totalItems = $this->_model->count();
    $result->items = $model->all();

    return $result;
}

Then I've got a pagination class that returns this:


public function ($std) 
{
  return new LengthAwarePaginator(
            $std->items,
            $std->totalItems,
            10,
            Paginator::resolveCurrentPage(),
            ['path' => Paginator::resolveCurrentPath()]);
}

Then in my controller:


$this->pagination->toLengthAware($this->userRepo->paginate());

Is this ok?

0 likes
0 replies

Please or to participate in this conversation.