Skip to content
Advertisement

Call to a member function lastPage() on int using laravel 6

i am starting to create an ads site, i display 36 products images in a blade index with success ,but when i try add pagination i get this error Call to a member function lastPage() on int .

AnnoncesController.php

public function index()
{
        $categories     = Category::all();
        $annonces       = Annonce::paginate(36);
        $paginator      = 36;
        return view('landing-page')->with([
            'categories'     => $categories,
            'annonces'       => $annonces,
            'paginator'      => $paginator
        ]);
}

landing-page.blade.php

<div class="pagination pagination-sm pull-right">
                        @if($annonces->hasPages())
                            {{ $annonces->links() }}
                        @endif
                        </div>
                        @if ($paginator->lastPage() > 1)
                            <ul class="pagination">
                                @if(($paginator->currentPage() > 1))
                                    <li class="{{ ($paginator->currentPage() == 1) ? '' : '' }}">
                                        <a href="{{ $paginator->url(1) }}"> << </a>
                                    </li>
                                @endif
                                @for ($i = 1; $i <= $paginator->lastPage(); $i++)
                                    <li class="{{ ($paginator->currentPage() == $i) ? 'current' : '' }}">
                                        <a href="{{ $paginator->url($i) }}">{{ $i }}</a>
                                    </li>
                                @endfor
                                @if(($paginator->currentPage() != $paginator->lastPage()))
                                    <li class="{{ ($paginator->currentPage() == 1) ? '' : '' }}">
                                        <a href="{{ $paginator->url($paginator->currentPage()+1) }}"> >> </a>
                                    </li>
                                @endif
                            </ul>
                            @endif   

Advertisement

Answer

You are calling $paginator->lastPage(); and $paginator is an integer, it should be $annonces->lastPage();

and this section should be

 @if ($annonces->lastPage() > 1)
    <ul class="pagination">
        @if(($annonces->currentPage() > 1))
            <li class="{{ ($annonces->currentPage() == 1) ? '' : '' }}">
                <a href="{{ $annonces->url(1) }}"> << </a>
            </li>
        @endif
        @for ($i = 1; $i <= $annonces->lastPage(); $i++)
            <li class="{{ ($annonces->currentPage() == $i) ? 'current' : '' }}">
                <a href="{{ $annonces->url($i) }}">{{ $i }}</a>
            </li>
        @endfor
        @if(($annonces->currentPage() != $annonces->lastPage()))
            <li class="{{ ($annonces->currentPage() == 1) ? '' : '' }}">
                <a href="{{ $annonces->url($annonces->currentPage()+1) }}"> >> </a>
            </li>
        @endif
    </ul>
@endif
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement