Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

orosznyet's avatar

Paginator class is missing (L5)

I just tried to use the following code (just for testing):

$data = [
    'items'      => ['1'],
    'totalItems' => 100,
    'perPage'    => 20,
    'pagination' => null,
    ];

$data['pagination'] = \Paginator::make( $data['items'], $data['totalItems'], $data['perPage'] );

And I got this very simple and usually easy-to-fix error:

Class 'Paginator' not found

What else should I do?

0 likes
16 replies
Moe's avatar

You're in the wrong namespace i guess, it should be

\Illuminate\Pagination\Paginator::make(....
orosznyet's avatar

Ops, I forgot to mention that I tried that as well but then I got the following problem:

Call to undefined method Illuminate\Pagination\Paginator::make()
Moe's avatar
public function products(Paginator $paginator)
{
    $products = [];
    return $paginator->make($products, count($products), '10');
}
adnan's avatar

Pagination class Laravel 5 not have make method.

What laravel version is used?

pmall's avatar

L5 :

$paginator = new Illuminate\Pagination\LengthAwarePaginator($items, $total, $per_page);
bobbybouwmann's avatar
Level 88

A more advanced sample:

// Get all items you want
$items = Item::all();

// Get the current page from the url if it's not set default to 1
$page = Input::get('page', 1); 

// Number of items per page
$perPage = 10;

// Start displaying items from this number;
$offSet = ($page * $perPage) - $perPage; // Start displaying items from this number

// Get only the items you need using array_slice (only get 10 items since that's what you need)
$itemsForCurrentPage = array_slice($items->toArray(), $offSet, $perPage, true);

// Return the paginator with only 10 items but with the count of all items and set the it on the correct page
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage, $page);

If you have something like filters on your site or other parameters in your url you will lose them when you start paginating, you fix this you by adding this:

return new LengthAwarePaginator($items, count($items), $perPage, $page, ['path' => Request::url(), 'query' => Request::query()]);

And you can just render the the pagination like this

{!! $items->render() !!}
2 likes
pmall's avatar

@bobbybouwmann It think it is wrong the purpose of paginator class is to handle this logic. If you pass it the total number of items, the items per page, and the current page all this will be calculated by the paginator.

bobbybouwmann's avatar

@pmall Mmmh you might be right but I just wanted to show a way where you only pass the items you will be displaying anyway instead of passing everything!

pmall's avatar

@bobbybouwmann I think the point is to pass everything and let the paginator handle the logic. But I may be wrong your post makes me doubt about it now lol

bobbybouwmann's avatar

Hehe :P Well I had some problems with getting the correct values from the pagination object since I was creating the paginator by myself.. I came up with this and it's works fine for me :P

The paginate function from the query builder is doing the same thing except for the array_slice part

public function paginate($perPage = null, $columns = ['*'])
{
    $total = $this->query->getCountForPagination();

    $this->query->forPage(
        $page = Pagination::resolveCurrentPage(),
        $perPage = $perPage ?: $this->model->getPerPage()
    );

    return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [
        'path' => Paginator::resolveCurrentPath(),
    ]);
}
pmall's avatar

Yes it is normal it only query the requested items but I was thinking if the number of items was higher than the per page value the paginator would display the correct ones by itself.

orosznyet's avatar

@bobbybouwmann @pmall Thank you guys for the help, I really appreciate it. Even though I made a few changes you showed me the right direction. (Dropbox API gives me huge chunks of data so this was a "must have").

mkwsra's avatar

@bobbybouwmann and other guys, Where should I place this? and the result that I want is collection not array, is that possible?

Please or to participate in this conversation.