How can I use or create a pagination in REST API in Laravel?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Mastering Pagination in REST API Using Laravel Body:

Creating an effective and user-friendly application involves providing an optimized experience for users to consume your data. In this blog post, we'll discuss how to use or create pagination in a Rest API using Laravel. This will help you display large datasets efficiently while giving users the option to navigate through multiple pages of information without experiencing performance issues.

Why Paginate Data?

Paginating data helps in optimizing application performance and making the user interface more manageable. If you display all records at once, it could lead to slow loading times or overwhelm users with too much information. Instead, dividing these records into pages ensures smooth navigation without any issues. It also helps when handling large datasets as users can access data in smaller chunks rather than being presented with everything upfront.

Methods for Implementing Pagination

There are several approaches you could use to implement pagination: 1. Manual Pagination: This approach involves handling the page navigation and displaying data manually in your code. You can create a simple interface where users choose which page they want to view. However, this method does not utilize Laravel's built-in functions and may be time-consuming. 2. Using the paginate() Method: Laravel provides the paginate() function that simplifies the process of listing data in paginated pages. This method retrieves the specified number of items from a model and uses it to display data. It is useful when you want to display a fixed number of items regardless of the total count. 3. Using Eloquent Relations: Another popular approach involves customizing your Eloquent models to work with relationships. In this case, you can define a relationship called 'getPaginatedList()' that returns paginated data from within a related model. This method allows for more flexibility and control over the data displayed.

Implementing Pagination in Rest API

Let's look at how to implement pagination with the paginate() method: 1. Retrieve all categories from the database and paginate them using the paginate() function, as shown below:
$getCategories = Category::paginate();
2. Loop through each category and convert it into an array called 'listCat' with relevant information such as ID, name, description, image URL, etc. This allows you to easily work with the data in your Rest API:
$listCat = array();
    foreach($getCategories as $val){
        $cateName['id'] = $val->id;
        $cateName['name'] = $val->name;
        $cateName['description'] = $val->description;
        $cateName['image'] = url('/').'/public/assets/images/category/'.$val->image;
        $listCat[] = $cateName;
    }
3. Return the response as a JSON object containing the status, message, and data list:
if(!empty($listCat)){
       $respons['status'] = 'true';
       $respons['message'] = 'Categories list';
       $respons['data list']=$listCat;
   }else{
      $respons['status'] = 'false';
      $respons['message'] = 'Category is not found.'; 
   }
return Response::json($respons,200);
Using this approach, you can easily manage your page navigation and display data in a more organized manner without compromising performance or user experience.

Conclusion

In conclusion, pagination is essential in ensuring that your Rest API delivers an efficient and user-friendly interface for accessing large datasets. By using Laravel's built-in functions like paginate(), you can achieve this seamlessly while keeping your code clean and well-organized. Remember to explore different methods, such as manual pagination or Eloquent relations, based on your specific needs for data handling in future projects.