abhijitghogre's avatar

Laravel 5 - Manual pagination

Paginator::make() method doesn't exist in Pagination class anymore.

Is there any workaround to make manual pagination work in Laravel 5?

0 likes
11 replies
abhijitghogre's avatar

I get this:

ReflectionException in Container.php line 745:
Class paginator does not exist

This is how I am using it:

use App\Product;
use Illuminate\Support\Facades\Paginator;
use Illuminate\Support\Facades\Input;

class DemoController extends Controller
{

    public function products()
    {

        $products = [......... // a big array ];

        return Paginator::make($products, count($products), Input::get('limit') ?: '10');

    }
}

PS: I corrected the typo in the question. @bonzai

Marwelln's avatar
Level 4

Try with this in your products method:

public function products(Paginator $paginator)
{
    $products = [];
    return $paginator->make($products, count($products), Input::get('limit') ?: '10');
}
2 likes
abhijitghogre's avatar

Works!!!

Crap! How did I forget Dependency Injection, the greatest gift of Laravel 5.

Thanks @Marwelln

But I wonder why it did not work the Facades way!

Marwelln's avatar

Input is available with just the use of use Input;, it's probably the same with Paginator, use Paginator; instead of use Illuminate\...\Paginator.

mikevrind's avatar

I'm currently struggling with the same issue. When I use the answer provided I get an error about an undefined method 'make'.

My controller:

use use Illuminate\Support\Facades\Paginator;

class PageController extends Basecontroller
{
     public function anyPage( Paginator $paginator )
     {
        dd($paginator->make($testArray, count($testArray), 2));
     }
}

Results in: Call to undefined method Illuminate\Support\Facades\Paginator::make()

I also tried to replace the use statement with just 'use Paginator'. But this gives the same error.

mikevrind's avatar

Owh, A colleague just gave me the info I needed. You can just call paginate on an Eloquent model :)

public function getIndex()
 {
  return self::Online()->OrderDate()->paginate(15, ['id', 'title', 'date']);
 }

But I also updated the laravel/framework and the laravel/laravel today, that also didn't solve the issue I encountered earlier.

eagle_arg's avatar

Hi there is my code for pagination: Use in blade @include('pagination.default', ['paginator' => $users]) if u want to put some filters in your url u can use appends @include('pagination.default', ['paginator' => $users->appends(Request::only(['surname'])), ])

Views/pagination/default.blade.php

@if ($paginator->lastPage() > 1)
    <ul class="pagination">
        <!-- si la pagina actual es distinto a 1 y hay mas de 5 hojas muestro el boton de 1era hoja -->
        <!-- if actual page is not equals 1, and there is more than 5 pages then I show first page button -->
        @if ($paginator->currentPage() != 1 && $paginator->lastPage() >= 5)
            <li>
                <a href="{{ $paginator->url($paginator->url(1)) }}" >
                    <<
                </a>
            </li>
        @endif

        <!-- si la pagina actual es distinto a 1 muestra el boton de atras -->
        @if($paginator->currentPage() != 1)
            <li>
                <a href="{{ $paginator->url($paginator->currentPage()-1) }}" >
                    <
                </a>
            </li>
        @endif

        <!-- dibuja las hojas... Tomando un rango de 5 hojas, siempre que puede muestra 2 hojas hacia atras y 2 hacia adelante -->
        <!-- I draw the pages... I show 2 pages back and 2 pages forward -->
        @for($i = max($paginator->currentPage()-2, 1); $i <= min(max($paginator->currentPage()-2, 1)+4,$paginator->lastPage()); $i++)
                <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
                    <a href="{{ $paginator->url($i) }}">{{ $i }}</a>
                </li>
        @endfor

        <!-- si la pagina actual es distinto a la ultima muestra el boton de adelante -->
        <!-- if actual page is not equal last page then I show the forward button-->
        @if ($paginator->currentPage() != $paginator->lastPage())
            <li>
                <a href="{{ $paginator->url($paginator->currentPage()+1) }}" >
                    >
                </a>
            </li>
        @endif

        <!-- si la pagina actual es distinto a la ultima y hay mas de 5 hojas muestra el boton de ultima hoja -->
        <!-- if actual page is not equal last page, and there is more than 5 pages then I show last page button -->
        @if ($paginator->currentPage() != $paginator->lastPage() && $paginator->lastPage() >= 5)
            <li>
                <a href="{{ $paginator->url($paginator->lastPage()) }}" >
                    >>
                </a>
            </li>
        @endif
    </ul>
@endif

Arda's avatar

@daniel_luevano To achieve that it'd be like this:

<?php

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Input;

Route::get('arda', function(){
    $pagination = new Paginator(range(1,200), 10, Input::get('page'));
    $pagination->setPath('arda');
    return $pagination->render();
});

so the setPath method sets the path, so he links.

1 like

Please or to participate in this conversation.