11

Here is my code where I am showing the paginations of my blog posts !!

<div class="clearfix">
    {{ $posts->render() }}
    <a class="btn btn-secondary float-right" href="#">Older Posts &rarr;</a>
</div>

Here is the controller where the pagination function is called.

public function index()
{
    $posts = post::where('status',1)->paginate(2);
    return view('user.blog',compact('posts'));
}

I have tried lot, but my pagination displayed but not properly styled !! Why ??

Here I show what is happening above the black line !!

Muhammad Hamza
  • 213
  • 1
  • 4
  • 14

4 Answers4

62

To display the pagination links in your blade view:

{{ $posts->links() }}

Or if you need to specify Bootstrap 4:

{{$posts->links("pagination::bootstrap-4")}}

Laravel docs: https://laravel.com/docs/5.5/pagination

kerrin
  • 3,376
  • 1
  • 26
  • 36
16

For pretty boostrap 4 pagination, simply call this:

{{ $results->links( "pagination::bootstrap-4") }}
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
11

For Laravel 8

Laravel includes pagination views built using Bootstrap CSS. To use these views instead of the default Tailwind views, you may call the paginator's useBootstrap method within the boot method of your App\Providers\AppServiceProvider class:

use Illuminate\Pagination\Paginator;

public function boot()
{
    Paginator::useBootstrap();
}

Laravel docs: https://laravel.com/docs/8.x/pagination

migsAV
  • 289
  • 3
  • 11
6

To add your bootstrap-style to links, you can create your pagination.blade.php that contains something like this

<!-- Previous page symbol "<" Link -->

@if ($paginator->hasPages())
    <ul class="pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="page-item disabled">
                <a class="page-link" href="#" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                    <span class="sr-only">Previous</span>
                </a>
            </li>
@else
            <li class="page-item"><a href="{{ $paginator->previousPageUrl() }}" class="page-link" rel="prev">&laquo;</a></li>
@endif

{{-- Pagination Elements(represents page number such as 1,2,3) --}}
@foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
    @if (is_string($element))
            <li class="page-item disabled">{{ $element }}</li>
    @endif

    {{-- Array Of Links --}}
    @if (is_array($element))
        @foreach ($element as $page => $url)
                @if ($page == $paginator->currentPage())
                        <li class="page-item active">
                            <a href="#" class="page-link">{{ $page }}<span class="sr-only">(current)</span></a>
                        </li>
                @else
                        <li class="page-item">
                            <a href="{{ $url }}" class="page-link">{{ $page }}</a>
                        </li>
                @endif
                @endforeach
            @endif
        @endforeach

        {{-- Next Page symbol ">" Link --}}
        @if ($paginator->hasMorePages())
                <li class="page-item"><a href="{{ $paginator->nextPageUrl() }}" class="page-link" rel="next">&raquo;</a></li>
        @else
                <li class="page-item disabled">
                    <a class="page-link" href="#" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                        <span class="sr-only">Next</span>
                    </a>
                </li>
        @endif
    </ul>
    @endif

and than use

{{ $results->links('pagination') }}
Kart Av1k
  • 137
  • 1
  • 6