How to check if pagination links will be shown in laravel blade?

Viewed 4067

I'm using paginate for my pagination and I want to add custom markup for the $items->links() in Blade like this:

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

How do I check if the pagination links will be shown so that I won't print out an empty pagination-wrapper?

2 Answers

I found out that you can access the total number of items and the items per page displayed for pagination. After that, it's easy to check if there is only one page - meaning there will be no pagination links to be shown.

@if ($items->total() > $items->perPage())
    <div class="pagination-wrapper">
         {{ $items->links() }}
    </div>
@endif

On Laravel 5 or above there is even better and Laravel native way to check if pagination links will be shown in laravel blade.

@if ($items->hasPages())
    <div class="pagination-wrapper">
         {{ $items->links() }}
    </div>
@endif

From Laravel API documentation

+----------------------+-------------------------------------------------------------------+
|        Method        |                            Description                            |
+----------------------+-------------------------------------------------------------------+
| $results->hasPages() | Determine if there are enough items to split into multiple pages. |
+----------------------+-------------------------------------------------------------------+
Related