use Illuminate\Http\Request;
public function index(Request $request)
{
$posts = Post::paginate(10);
return response()->json([
'data' => $posts->items(), // The paginated items
'meta' => [
'current_page' => $posts->currentPage(),
'per_page' => $posts->perPage(),
'total' => $posts->total(),
'last_page' => $posts->lastPage(),
],
'links' => [
'next' => $posts->nextPageUrl(),
'previous' => $posts->previousPageUrl(),
],
]);
}
{
"data": [
{ "id": 1, "title": "Post 1" },
{ "id": 2, "title": "Post 2" }
],
"meta": {
"current_page": 1,
"per_page": 10,
"total": 50,
"last_page": 5
},
"links": {
"next": "http://example.com/api/posts?page=2",
"previous": null
}
}