简体   繁体   中英

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

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?

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. |
+----------------------+-------------------------------------------------------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM