chintan's avatar
Level 13

LengthAwarePaginator gives keys along with data on page 2

I am using LengthAwarePaginator for pagination for custom array. First page gives the expected result however second page displays the data along with keys.

Here's the code:

            $currentPage = LengthAwarePaginator::resolveCurrentPage();
            $collection = new Collection($tempArray);
            $perPage = config('kh.api_record_display_limit');
            $currentPageSearchResults = $collection->slice(($currentPage-1) * $perPage, $perPage)->all();
            $paginatedSearchResults= new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);

$response = array(
                                'status'  => 'success',
                                'message' => 'Data retrieved successfully',
                                'data' => $paginatedSearchResults
            );  
            return Response::json($response,200);

JSON Reponse for the first page :

{
    "status": "success",
    "message": "Data retrieved successfully",
    "data": {
        "total": 8,
        "per_page": 5,
        "current_page": 1,
        "last_page": 2,
        "next_page_url": "/?page=2",
        "prev_page_url": null,
        "from": 1,
        "to": 5,
        "data": [
            {
                "date": "2016-06-13",
                "blood_pressure_diastolic": "90",
                "blood_pressure_systolic": "200",
                "track_type": "Monthly"
            },
            {
                "date": "2016-02-02",
                "blood_pressure_diastolic": "80",
                "blood_pressure_systolic": "140",
                "track_type": "Monthly"
            },
            {
                "date": "2016-02-16",
                "blood_pressure_diastolic": "80",
                "blood_pressure_systolic": "120",
                "track_type": "Monthly"
            },
            {
                "date": "2016-02-19",
                "blood_pressure_diastolic": "80",
                "blood_pressure_systolic": "120",
                "track_type": "Monthly"
            },
            {
                "date": "2016-02-26",
                "blood_pressure_diastolic": "120",
                "blood_pressure_systolic": "100",
                "track_type": "Monthly"
            }
        ]
    }
}

Response for second page:

{
    "status": "success",
    "message": "Data retrieved successfully",
    "data": {
        "total": 8,
        "per_page": 5,
        "current_page": 2,
        "last_page": 2,
        "next_page_url": null,
        "prev_page_url": "/?page=1",
        "from": 6,
        "to": 8,
        "data": {
            "5": {
                "date": "2016-02-29",
                "blood_pressure_diastolic": "80",
                "blood_pressure_systolic": "120",
                "track_type": "Monthly"
            },
            "6": {
                "date": "2016-01-08",
                "blood_pressure_diastolic": "120",
                "blood_pressure_systolic": "80",
                "track_type": "Never"
            },
            "7": {
                "date": "1970-01-01",
                "blood_pressure_diastolic": "23",
                "blood_pressure_systolic": "23",
                "track_type": "Monthly"
            }
        }
    }
}

Any suggestion on how to get rid of keys on second page for displaying data ?

0 likes
1 reply
chintan's avatar
chintan
OP
Best Answer
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,
        ];
    }

Please or to participate in this conversation.