Home > Software design >  How would paginate a json response in my GET request without the use of models/databases?
How would paginate a json response in my GET request without the use of models/databases?

Time:02-11

I'd like to know how I can paginate multiples of 10 with the given response in my GET request when it's not using databases/models.

I've searched around but I'm mostly seeing examples of database/model paginations and not json response paginations.

Below's what I've tried (along with so many other ways but I've ran out of options and hit a wall) but to no avail.

I know there's a way to do this simply but I just can't seem to wrap my head around it. Any feedback would be appreciated :).

$response = Http::get(env('endpoint'), $queryParams);

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

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

$results->paginate(10); // multiples of ten.  I know this is the Model way of doing it but I can't find any other way to achieve this.

CodePudding user response:

You can collect the information:

collect($results)

Then paginate using ->skip(number_to_skip) and then ->take(number_to_take);

Then just return it like you're doing. It'd be something like:

$page = $request->page;
$take = 10;
$skip = ($page - 1) * $take;
$resultsCount = collect($results)->count();
$results = collect($results)->skip($skip)->take($take)->all();

CodePudding user response:

May be you can use de chunk method of collection

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