35

am trying to port my laravel4 application to laravel 5 . In the previous version i could use the following method for generating pagination urls .

In controller:

$this->data['pages']= Page::whereIn('area_id', $suburbs)->where('score','>','0')->orderBy('score','desc')->paginate(12);

and after sharing the data array with the view, i could user

In views :

{{$pages->links()}}

In laravel 5 the doing that results in following error

ErrorException in AbstractPaginator.php line 445:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'links'

not sure what i am missing here, can somebody help ?

4
  • 3
    I suggest to wait until Laravel 5 is stable because it has some bugs and the base code can change. Commented Jan 16, 2015 at 9:52
  • @ClaudioLudovicoPanetta i think it will come out this month :) . just wanted to be in sync with the changes . using 4.2 for production though . Commented Jan 16, 2015 at 9:54
  • 1
    your problem seems more a Laravel issue than yours, I will try to replicate you problem for a solution, brb. Commented Jan 16, 2015 at 10:02
  • @ClaudioLudovicoPanetta thanks :) . will wait and see. else will add a issue at github Commented Jan 16, 2015 at 10:06

8 Answers 8

73

In Laravel 5 there is no method "links" you can try this

{!! $pages->render() !!}
Sign up to request clarification or add additional context in comments.

4 Comments

{!!$pages->render()!!} did the trick , the other syntax , in laravel5 escapes html . thanks
Finally a solution to a simple pagination, not this new "create 5 php files, implement a class and write 200 lines of code". FINALLY! Thank you.
this doesn't work. Is there anything that I need to prepare first before using it?
@muhaimin You need to attach paginate() function while accessing data from database. for example: Page::whereIn('area_id', $suburbs)->orderBy('score','desc')->paginate(12); and pass the variable to the view.
9

In other frameworks, pagination can be very painful. Laravel 5 makes it a breeze. For using it, first you have to make a change in your controller code where you calling data from the database:

 public function index()
{       
        $users = DB::table('books')->simplePaginate(5);
        //using pagination method
        return view('index', ['users' => $users]);
}

...after that you can use this code:

<?php echo $users->render(); ?>

That will make you use simple Laravel 5 beauty.

Comments

6

Pagination Laravel 5.6.26, for the pagination the controller are :

Controller code (https://laravel.com/docs/5.6/pagination#basic-usage)

    $posts = Post::orderBy('created_at','desc')->paginate(10);
    return view('posts.index')->with('posts', $posts);

Front end into blade (view) (https://laravel.com/docs/5.6/pagination#displaying-pagination-results)

   {{ $users->links() }}

Comments

5
$items = SomeDataModel->get()->paginate(2); // in your controller

@foreach(items as $item)   // in your view.blade file
....echo some data here
@endforeach

<div class="pagination">
    {{ $items->render() }} or {{ $items->links() }}
</div>

Use the array name (items) in the render() or links() method NOT the array item. It worked for me.

Comments

2

Best Way Pagination And Simple Code

 $limit = 10;
 $page_no = ($request->input('**page_no**') * $limit) - $limit;


   $data = User::whereraw($whereraw)
                  ->limit(**$limit**)
                  ->offset(**$page_no**)
                  ->get();

Comments

1

use

$users = User::where('id', Auth::user()->id)->simplePaginate(20);

and use in blade

{!! $users->render() !!}

1 Comment

Undefined variable: pages
-1
@if ($posts->lastPage() > 1)
        <nav aria-label="Page navigation">
            <ul class="pagination">
                @if($posts->currentPage() != 1 && $posts->lastPage() >= 5)
                <li>
                    <a href="{{ $posts->url($posts->url(1)) }}" aria-label="Previous">
                        <span aria-hidden="true">First</span>
                    </a>
                </li>
                @endif
                @if($posts->currentPage() != 1)
                <li>
                    <a href="{{ $posts->url($posts->currentPage()-1) }}" aria-label="Previous">
                        <span aria-hidden="true">&#x3C;</span>
                    </a>
                </li>
                @endif
                @for($i = max($posts->currentPage()-2, 1); $i <= min(max($posts->currentPage()-2, 1)+4,$posts->lastPage()); $i++)
                @if($posts->currentPage() == $i)
                <li class="active">
                @else
                <li>
                @endif
                    <a href="{{ $posts->url($i) }}">{{ $i }}</a>
                </li>
                @endfor
                @if ($posts->currentPage() != $posts->lastPage())
                <li>
                    <a href="{{ $posts->url($posts->currentPage()+1) }}" aria-label="Next">
                        <span aria-hidden="true">&#x3E;</span>
                    </a>
                </li>
                @endif
                @if ($posts->currentPage() != $posts->lastPage() && $posts->lastPage() >= 5)
                <li>
                    <a href="{{ $posts->url($posts->lastPage()) }}" aria-label="Next">
                        <span aria-hidden="true">Last</span>
                    </a>
                </li>
                @endif
            </ul>
        </nav>
        @endif

1 Comment

Give some explanation, the answer should be explained atleast a bit.
-7

Hi there is my code for pagination: Use in view:
@include('pagination.default', ['paginator' => $users])

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

1 Comment

look that i didn't copy well... the code starts before the gray area.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.