Jan 24, 2017
0
Level 1
LengthAwarePaginator data field is an Object not an Array
I'm using the LengthAwarePaginator façade in this way because I have to paginate a raw query, so I cannot simply concatenate the paginate() method.
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$perPage = 10;
$results = $teachers->slice(($currentPage-1) * $perPage, $perPage)->all();
$pagination = new LengthAwarePaginator(
$results,
count($teachers),
$perPage,
$currentPage,
['path' => LengthAwarePaginator::resolveCurrentPath()]
);
For the first page the result is okay:
{
"total":217,"
per_page":10,
"current_page":1,
"last_page":22,
"next_page_url":"http:..",
"prev_page_url":null,
"from":1,
"to":10,
"data":[] //Array
}
...but from the second page...
{
"total":217,"
per_page":10,
"current_page":2,
"last_page":22,
"next_page_url":"http:..",
"prev_page_url":"http",
"from":11,
"to":20,
"data":{
"10":{},
"11":{},
...
} //Object
}
This is a very mess for the api consumer (a Ionic2 app), which isn't able to concat in the right way the first portion of the data with the second one etc...
Any ideas?
Thanks :)
Please or to participate in this conversation.