Paginated API Responses in Laravel 12: The Right Way

Author

Kritim Yantra

Jun 25, 2025

Paginated API Responses in Laravel 12: The Right Way

“Imagine loading 10,000 records in a mobile app — not fun, right? That’s where pagination saves the day!”

If you’re building APIs in Laravel 12, chances are you’ll run into the need for pagination. Whether you're listing blog posts, users, products, or comments — pagination keeps your API fast, clean, and user-friendly.

In this easy-to-follow guide, you'll learn how to:

  • Paginate your Laravel API responses
  • Use API Resources with pagination
  • Customize the pagination output

Let’s dive in! 🏊


🧠 What Is Pagination?

Imagine you're in a library with 1,000 books. Instead of listing all the books on one long page, the librarian hands you a catalog — 10 books per page.

That’s pagination — breaking large data into small, manageable chunks.


🛠️ Step 1: Update Your Controller to Use paginate()

Let’s assume you already have a PostController and a Post model.

Replace this:

return Post::all();

With this:

return Post::paginate(10);

This will return 10 posts per page by default.


🔍 Sample Paginated Response

{
  "current_page": 1,
  "data": [
    { "id": 1, "title": "Post One", ... },
    { "id": 2, "title": "Post Two", ... }
  ],
  "first_page_url": "http://yourapp.test/api/posts?page=1",
  "last_page": 5,
  "last_page_url": "http://yourapp.test/api/posts?page=5",
  "per_page": 10,
  "total": 50
}

That’s a lot of helpful info returned automatically!


📦 Step 2: Use Resources with Pagination

If you're using API Resources (which you should for clean API responses), here's how to use them with pagination.

Update your index() method:

use App\Http\Resources\PostResource;

public function index()
{
    return PostResource::collection(Post::paginate(10));
}

Laravel will wrap the pagination metadata automatically, like:

{
  "data": [
    { "id": 1, "title": "Hello", "content": "..." },
    ...
  ],
  "meta": {
    "current_page": 1,
    "last_page": 5,
    "per_page": 10,
    "total": 50
  },
  "links": {
    "first": "...",
    "last": "...",
    "prev": null,
    "next": "..."
  }
}

🧰 Step 3: Customize Your Pagination

Want to change the number of items per page?

You can make it dynamic using the query string:

public function index(Request $request)
{
    $limit = $request->get('limit', 10); // default = 10
    return PostResource::collection(Post::paginate($limit));
}

Now you can call:

GET /api/posts?limit=5

🔐 Bonus Tip: Add Simple Pagination

If you only care about next/previous, use:

Post::simplePaginate(10);

This returns a lighter payload, useful for mobile apps or simple UIs.


🧪 Testing Pagination

Use Postman or your favorite API client:

  1. GET /api/posts
  2. GET /api/posts?page=2
  3. GET /api/posts?limit=5&page=3

🎯 You’ll get a smaller, faster response each time.


🧠 Summary: Why Pagination Is a Must-Have

  • 📉 Reduces server load and bandwidth
  • 📱 Improves performance on mobile/web
  • 📦 Keeps API response clean and fast
  • ️ Works perfectly with Laravel API Resources

✅ Final Thoughts

Pagination is not just a nice-to-have — it’s essential for scalable and efficient APIs. With Laravel 12, it's super simple to implement and customize.

Ajay Yadav

Ajay Yadav

Senior Full-Stack Engineer

7 + Years Experience

Transforming Ideas Into Digital Solutions

I architect and build high-performance web applications with modern tech:

Laravel PHP 8+ Vue.js React.js Flask Python MySQL

Response time: under 24 hours • 100% confidential

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts