How to Paginate Laravel 10 API Resources?

18-Dec-2023

.

Admin

How to Paginate Laravel 10 API Resources?

Hello Dev,

This tutorial will provide an example of how to paginate Laravel 10 API resources. Here you will learn how to paginate Laravel 10 API resources. I would like to share with you how to use pagination in API in Laravel 10. you can understand the concept of how to make API resources in Laravel 10. Let's get started with API resources with pagination in Laravel 10.

Laravel provides a simple and convenient way to paginate Eloquent models and collections. To paginate a collection of Eloquent models, you can use the paginate()


method. This method will return a paginated collection, which includes the following information:

1. The current page

2. The total number of pages

3. The number of results per page

4. The total number of results

5. Links to the next and previous pages

To paginate a collection of API resources, you can use the collection() method on your resource class. This method will return a resource collection, which represents a paginated collection of resources.

For example, the following code shows how to paginate a collection of user resources.

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\UserResource;

use App\Models\User;

class UserController extends Controller

{

/**

* write code for.

*

* @param \Illuminate\Http\Request

* @return \Illuminate\Http\Response

* @author <>

*/

public function index()

{

$users = User::paginate(10);

return UserResource::collection($users);

}

}

This code will return a JSON response with the following structure.

{

"data": [

{

"id": 1,

"name": "John Doe",

"email": "john.doe@example.com"

},

{

"id": 2,

"name": "Jane Doe",

"email": "jane.doe@example.com"

}

],

"links": {

"first": "https://example.com/api/users?page=1",

"last": "https://example.com/api/users?page=5",

"prev": null,

"next": "https://example.com/api/users?page=2"

},

"meta": {

"current_page": 1,

"from": 1,

"last_page": 5,

"path": "https://example.com/api/users",

"per_page": 10,

"to": 10,

"total": 50

}

}

The 'data' key contains the paginated collection of user resources, while the 'links' key holds references to the next and previous pages. The 'meta' key includes additional pagination details, such as the current page, the total number of pages, and the number of results per page.

You can customize the pagination response by incorporating extra metadata into your resource collection class. For instance, the following code illustrates how to include the total number of users in the pagination response.

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\UserResource;

use App\Models\User;

class UserCollection extends ResourceCollection

{

/**

* write code for.

*

* @param \Illuminate\Http\Request

* @return \Illuminate\Http\Response

* @author <>

*/

public function toArray($request)

{

return [

'data' => $this->collection->transform(function (User $user) {

return new UserResource($user);

}),

'meta' => [

'total_users' => $this->collection->count(),

],

];

}

}

This code will add a total_users key to the meta key of the pagination response.

Paginating Laravel API resources is a straightforward process. By employing the paginate() and collection() methods, you can effortlessly deliver paginated collections of resources to your clients.

I hope it can help you...

#Laravel 10