How would I paginate multiples of 20 correctly based on query params?

Viewed 29

When the user inputs an offSet value in the query param in the URL, oddly enough, when $take = 20; and $page = 1; - this is when it displays the relevant data, but I'm not sure why numbers like 2, 4, 5, 10 won't work. So what's wrong with my code?

$results = $response->json()['results'];

collect($results);
$page = $request->get('offSet');
$take = 20;
$skip = ($page - 1) * $take;
$paginationResults = collect($results)->skip($skip)->take($take)->all();
    
dd(paginationResults);
1 Answers

you can use the chunk methods of collection

$results = collect($result);
$outputs = $results->chunck(20)[$request->input('offset')];
Related