'How can I work around pagination using chunk because of repeated errors being thrown?
I'm trying to paginate my json response in terms of multiples of 20 but for some reason, I'm getting Error: Call to a member function chunk() on array.
To clarify, $results has data in it and $request->get('offset'); has a value. Is there something I'm doing wrong? I'm seeing examples when searching on SO where people are doing this on a collection but for some reason, it isn't working for me. How can I make this work?
Note: If I do dd($results['num_results']);, it displays 360. I feel like I need to use this some how but I'm not sure how. I'm trying to achieve this in the most Laravel way possible. Also note, no database is being used - this is simply a get request on an external API.
Thanks in advance.
collect($results);
$outputs = $results->chunk(20)[$request->get('offset')];
dd($outputs);
Solution 1:[1]
$results is still an array. You are not setting the new Collection you are creating to a variable: collect($results). This does not change $results. So you are calling chunk on the array, $results. Call chunk on the Collection instead:
$collection = collect($results);
$outputs = $collection->chunk(20)...;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lagbox |
