Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ap1234's avatar

How to Paginate an Array in Livewire?

Hey guys I have an array of products which are fetched dynamically and displayed in a table format, How can I add pagination to the array?

0 likes
8 replies
jlrdw's avatar

Have you tried to use database pagination instead of an array?

ap1234's avatar

@jlrdw I have two tables in view, Table 1 and Table 2. Table 2 has all the products which comes from the DB. Now I have a functionality which on the click of the row button that product is added in the Table 1 which is an array. There can be more than 100 products so I wanted to paginated that.

Tray2's avatar

Like @jlrdw writes, you should handle the pagination in the database, but if that isn't and option, you need to handle it client side with javascript. It's not as hard as it may sound.

You need to keep track of this

  • Items per page
  • Total number of items
  • Total number of pages
  • Current page

Let's say that you want ten per page and you have 100 records.

  1. Items per page: 10
  2. Total number of items: 100
  3. Total number of pages: 100/10
  4. Current page: 0

Then you can use this calculation to know which items to display.

current page + number of items = last item to display

last item to display - items per page = first item to display
ap1234's avatar

@Tray2 Hey! Thankyou for your reply! I will try this out. I thought there might be a way to paginate it from livewire itself. Or I could change the flow by creating a temporary table to store the selected products and then paginate it using DB pagination. Context in reply to @jlrdw

Hamze-Ammar's avatar

I faked a Sushi model out of my array and was to able to fully utilize Livewire pagination functionality.

Please or to participate in this conversation.