Modify the response structure to include metadata and links:
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(),
],
]);
}