1
votes

I need a custom pagination but in my case, i do not use eloquent for fetching data. I use an API where i fetch data.

I've been dealing with Pagination class, but as far as i know, it takes a collection and paginate it. That's not what i need.

What i need is creating a paginate object based on a subset of records gotten by a search query. Let's say such query has a total of 10000 records and I only get an array of 50 items, so each paginate has 50 elements. So, i need to create the pagination links based on this info.

Is there any way of accomplish it?

EDIT:

$models = array('total'          => $n_results,
                'per_page'      => 30,
                'current_page'  => 1,
                'last_page'     => ceil($n_results/30),
                'next_page'     => "******",
                'prev_page'     => "******",
                'from'          => 1,
                'to'            => 30,
                'data'          => $items);
1
just checking: you can't make the API generate paginated results, right? Like, in API side, using resources and wrapping everything in sth like {"data": [...],"links": {"first":...,"last":...,"prev":...,"next":...},"meta": {...}} is not an option, isn't?victorf
@victorf I can create such object but when inserting the pagination in Blade, it does not work.Apalabrados
but does this object have this same structure that I've mentioned?victorf
@victorf Have a look at the object i've been creating. The same as it's created using the pagination with Eloquent.Apalabrados
@victorf Finally I made my own pagination script.Apalabrados

1 Answers

0
votes

Based on what I understood here what I would do:

1- if you cannot ask the limit and offset for each call of the API and the API provides all the results to you at once but you want to show them 50 at a time, then I would create a temp_table and insert the data to it and then it's like it's on my own database then I would be able to sort,limit and offset by myself.

2- if the total result is not that much (like less than 500) and you want the user to not be overwhelmed by all the results together and you wanna show it to them 50 results at a time or 20 results at a time you can load all the result in blade by hide their element. (it's not what I would recommend but it would do the trick if you don't want a temp_table)