Courses

How to Build Laravel 11 API From Scratch

Data Pagination via API

Summary of this lesson:
- Implementing API pagination using Laravel's paginate()
- Understanding pagination metadata in API responses
- Consuming paginated API data in Vue.js frontend
- Using Laravel Vue Pagination package for frontend implementation

Let's talk about pagination for products. Now, on the home page, we have all the products loaded, which, in this case, is twenty products. But in the real world, shops could have hundreds and thousands of products. So we need to paginate them.


In a regular Laravel project, instead of using get(), you would use paginate().

app/Http/Controllers/Api/ProductController.php:

class ProductController extends Controller
{
public function index()
{
$products = Product::with('category')->get();
$products = Product::with('category')->paginate(9);
 
return ProductResource::collection($products);
}
}

And in the Blade, you would do $products->links(), and it would generate the pagination with links.

But in the API, we work only with the data. How do we tell the frontend the links for the pagination, how many records are there, how many pages, and what the next and previous pages are? When using Resources and returning a collection, Laravel handle...

The full lesson is only for Premium Members.
Want to access all 23 lessons of this course? (58 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord