Level 13
Solved. Here's the code:
Controller:
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$collection = new Collection($returnData);
$perPage = config('kh.api_record_display_limit');
// $currentPageSearchResults = $collection->slice(($currentPage-1) * $perPage, $perPage)->all();
// $paginatedSearchResults= new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);
$paginatedSearchResults= new LengthAwarePaginator($collection->forPage($currentPage,$perPage),$collection->count(), $perPage,$currentPage);
DB::commit();
$response = array(
'status' => 'success',
'message' => 'Data retrieved successfully',
'data' => $paginatedSearchResults
);
return Response::json($response,200);
LengthAwarePanigator.php
public function toArray()
{
$data;
if($this->currentPage > 1)
{
$tempdata = $this->items->toArray();
foreach ($tempdata as $key => $value)
{
$data[] = $value;
}
}
else
{
$data = $this->items->toArray();
}
return [
'total' => $this->total(),
'per_page' => $this->perPage(),
'current_page' => $this->currentPage(),
'last_page' => $this->lastPage(),
'next_page_url' => $this->nextPageUrl(),
'prev_page_url' => $this->previousPageUrl(),
'from' => $this->firstItem(),
'to' => $this->lastItem(),
'data' => $data,
];
}