Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually Creating a Paginator (Laravel 5)

I seem to be unable to manually create an instance of the paginator.

use Illuminate\Pagination\Paginator;

class Blah {
    public function index(Paginator $paginator)
    {
        // Build array
        $var = $paginator->make($array, $count, 200);
        return $var;
    }
}

From here I'm just getting Unresolvable dependency resolving [Parameter #0 [ <required> $items ]] in class Illuminate\Pagination\Paginator

like image 567
Sturm Avatar asked Mar 24 '15 19:03

Sturm


People also ask

How can we manually create pagination in Laravel?

Just to say (Laravel doc): When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. Could you explain parameters please? The parameters are discussed over the construct method: laravel.com/api/5.0/Illuminate/Pagination/…

Can I paginate an array in Laravel?

you can easily do it laravel pagination with array. step by step solution of laravel paginate from array. We will create our custom collection object with array and create pagination using laravel eloquent.


2 Answers

There is no more make() method in laravel 5. You need to create an instance of either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator . Take a look at documentation page, Creating A Paginator Manually part

http://laravel.com/docs/master/pagination

I guess it'll look something like this:

use Illuminate\Pagination\Paginator;

class Blah {
    public function index()
    {
        // Build array
        $array = [];
        return new Paginator($array, $perPage);;
    }
}

Also check this answer.

like image 78
mirzap Avatar answered Nov 10 '22 16:11

mirzap


In 2019, to manualy paginate items in laravel you should instantiate Illuminate\Pagination\Paginator class like this:

// array of items
$items = [ 'a' => 'ana', 'b' => 'bla', 'c' => 'cili' ]; 

// items per page
$perPage = 2; 

// let paginator to regonize page number automaticly
// $currentPage = null; 

// create paginator instance
$paginate = new Paginator($items, $perPage);

hope this helps.

like image 32
Amin Shojaei Avatar answered Nov 10 '22 16:11

Amin Shojaei