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

devkon98's avatar

How to paginate table with values from API

Hello i have this table with values from API and i want to make a paginate, this is my code

 <div class="list-table">
                        <div class="table-header">
                           <div class="row">
                              <div class="col-4">
                                 <h4 class="mb-0 font-d-bld text-white">Game</h4>
                              </div>
                              <div class="col-2">
                                 <h4 class="mb-0  font-d-bld text-white">Bet</h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white">X</h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white">Win</h4>
                              </div>
                           </div>
                        </div>
                        <div class="table-list">
                           <?php
                              $count =0;
                              foreach($values->bonuses as $value)
                              {  
                              	$count +=1;		
                              	if($count <= 7){
                              	?>
                           <div class="row list-item">
                              <div class="col-4">
                                 <h4 class="mb-0 font-d-bld text-white"><?php echo $count.")".$value->name;?></h4>
                              </div>
                              <div class="col-2">
                                 <h4 class="mb-0  font-d-bld text-white"><?php echo $value->bet_size;?></h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white"><?php echo $value->multiplier;?></h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white"><?php echo $value->payout;?></h4>
                              </div>
                           </div>
                           <?php
                              }
                              }
                              ?>
                        </div>
                        <div class="table-pagination-btn d-flex justify-content-between">
                           <div class="left-btn">
                              <a href="#"><i class="fa-solid fa-angle-left"></i></a>
                           </div>
                           <div class="pagination-number">
                              <ul class="ps-0 mb-0 d-flex">
                                 <li><a href="#" class="font-d-bld">1</a></li>
                                 <li><a href="#" class="font-d-bld">2</a></li>
                                 <li><a href="#" class="font-d-bld">3</a></li>
                              </ul>
                           </div>
                           <div class="right-btn"><a href="#"><i class="fa-solid fa-angle-right"></i></a></div>
                        </div>
                     </div>
0 likes
5 replies
LaryAI's avatar
Level 58

To paginate the table with values from API, you can use Laravel's built-in pagination feature. Here's how you can modify the code to achieve this:

  1. First, you need to fetch the data from the API and store it in a variable. You can do this in your controller method like this:
public function index()
{
    $values = // fetch data from API

    return view('your-view', compact('values'));
}
  1. Next, you can use Laravel's paginate method to paginate the data. Modify your controller method like this:
public function index()
{
    $values = // fetch data from API

    $values = collect($values->bonuses)->paginate(7);

    return view('your-view', compact('values'));
}

Here, we're using the collect method to convert the data into a collection, and then calling the paginate method on it to paginate the data with 7 items per page.

  1. In your view, you can loop through the paginated data like this:
@foreach ($values as $value)
    <div class="row list-item">
        <div class="col-4">
            <h4 class="mb-0 font-d-bld text-white">{{ $loop->iteration }}) {{ $value->name }}</h4>
        </div>
        <div class="col-2">
            <h4 class="mb-0 font-d-bld text-white">{{ $value->bet_size }}</h4>
        </div>
        <div class="col-3">
            <h4 class="mb-0 font-d-bld text-white">{{ $value->multiplier }}</h4>
        </div>
        <div class="col-3">
            <h4 class="mb-0 font-d-bld text-white">{{ $value->payout }}</h4>
        </div>
    </div>
@endforeach

Here, we're using the @foreach directive to loop through the paginated data, and the $loop->iteration variable to display the item number.

  1. Finally, you can use Laravel's built-in pagination links to display the pagination buttons. Replace the pagination section in your view with this:
{{ $values->links() }}

This will display the pagination links with the default styling. If you want to customize the styling, you can use the links method with a custom view like this:

{{ $values->links('your-custom-pagination-view') }}

Here, your-custom-pagination-view is the name of your custom pagination view file. You can create this file in the resources/views/vendor/pagination directory and customize the pagination links as per your requirements.

Pippo's avatar

Is suggest you to use controllers to manage the pagination and also to use blade views instead of plain HTML and <?php tags

Pippo's avatar

The 'AI' is ever the fastest ;-)

Please or to participate in this conversation.