lengthawarepaginator returns keys on page 2 code example

Example: forPage return keys on page 2

// On pages >= 2, the keys are returned since forPage returns an array with keys
// To fix this, just chain values() method.
$items_per_page = 10;
$current_page = LengthAwarePaginator::resolveCurrentPage();
$paginated_response = new LengthAwarePaginator(
  	collect($items)->forPage($current_page, $items_per_page)->values(),
  	count($items),
  	$items_per_page,
  	$current_page,
  	['path' => url('api/portfolios')]
);
return response()->json($paginated_response);

Tags:

Php Example