How to format pagination in laravel?

by tressie.damore , in category: PHP Frameworks , 6 months ago

How to format pagination in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 5 months ago

@tressie.damore 

Pagination in Laravel can be formatted using the links method provided by the built-in Paginator. By default, Laravel paginates results with a simple numeric pagination. However, you can customize the pagination format by using the link method in your Blade views.


Here is an example of how you can format pagination in Laravel:

1
2
3
4
5
@foreach ($items as $item)
    // Display item details
@endforeach

{{ $items->links() }}


This code will output a pagination control with a default format using numeric links. If you want to customize the pagination format, you can pass an array of options to the links method. For example:

1
{{ $items->links(['format' => 'Page {page} of {pages}']) }}


This will display the pagination control with a custom format showing the current page number and total number of pages.


You can also customize the previous and next page links by passing the prev and next options:

1
{{ $items->links(['prev_text' => 'Previous', 'next_text' => 'Next']) }}


These are just a few examples of how you can format pagination in Laravel. You can check the Laravel documentation for more options and customization possibilities.