Is there a way to convert pagintation to collection/array/json Laravel - laravel

Is there a way to convert a pagination to a collection/array to get all items like:
Users::where(...)->paginate(15); //paginated
Output: 15 per page
Users::where(..)->get(); //not paginated
Output: all Users per page
So i is it possible to do something like that:
Users::where(...)->paginate(15)->removePagination();
Wanted output: all Users per page

You have 2 choices:
either retrieve the complete query result by chaining ->get()onto your query.
call ->paginate() to retrieve a pagination instance, which automatically retrieves the first page, and it automatically links the example.com?page=1 url param.
(there are also some other functions that complete the query like ->pluck() or ->value() which might not be applicable to you)
As far as I'm aware you cannot revert to the complete collection instance when you have the pagination retrieved (\Illuminate\Contracts\Pagination\LengthAwarePaginator), nor does it make sense to do so since the idea is to retrieve as little data as possible such that your application stays fast.
Maybe you should explain what you're trying to do; for instance you can retrieve the total count in the paginator using SomeModel::query()->paginate()->total(), among other things. You might not need the complete query after all.

Related

Prisma 2: How to paginate fields of a .findUnique() query?

Description:
I have a seeProfile resolver, which gives back a profile of a particular user.
This user has a photos array and I want to paginate these photos and I can't figure out how. I have a workaround but I'd like to know a way - shall it exist - to paginate a field of this one user.
Workaround:
I can simply make a resolver for fetching pictures for a particular user with .findMany() and paginate them with take: and skip: . But then I make two queries instead of one. So, if anyone knows a way to paginate the photos inside findUnique query, please let me know, thanks!
Unfortunately, there's no way as of now to implement pagination inside the include relation in Prisma.
You will have to make two separate queries with Prisma, as you described in your workaround:
One findUnique query to fetch the user records.
One findMany query to fetch the approrpriate photo records.
I can simply make a resolver for fetching pictures for a particular user.
Perhaps it just makes sense to do both queries in the same resolver, to avoid two requests to the API.

Should list in GraphQL type always be paginated?

As per Relay client specification, and GraphQL specification I got that list should be implemented via Connection and Edges, to give ability to paginate it.
But what if I have a lift of nodes that I don't need to paginate? I mean, I always want to load all entries all the time and there is no business cases where I need only part of that list.
Should it be also paginated anyway?
No, pagination is not always necessary. There are many cases where you definitely don't want pagination. For example, a graphql query to get dropdown options for a form. So pagination is optional and depends on your use case. Trust your instinct for if you need pagination or not. You can always change it later.

how to get total results in laravel pagination?

I have collection $data=$qwery->paginate();
Further in the loop, I send an api request for each element and receive data, which I use with setAttribute to add to the current element of the collection.
In my blade file, I display the information in the form of a table. The last row of the table should contain the total amount of the field that we added using the api request for all elements on all pages. Can I get all collections of all pages in the controller and then calculate the amount using the standard collection method? I tried to use get() instead of pagination, but the request is taking too long, due to API. I can calculate the amount I need for the current page. But how can I find out the amount for all pages at once? I do not ask for a ready-made solution, just your ideas
Use $data->total() to get the total number of items in your paginated results.
Read more here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods

Yii CGridView pagination

I'm using CArrayDataProvider (which is basically a customized query i've created) that returns all the results (over 1000) from the database.
I'm using the results in the view but when i'm using the pagination it's going back to the controller for another query.
my question is: is there any way to move on the the next set of results (already part of the result array) without going to the controller and model again.
*My controller has a fairly advanced function which requires variables and parameters which i dont have in the view when trying to use standard AJAX request for the next page.
thanks,
Danny
my question is: is there any way to move on the the next set of
results (already part of the result array) without going to the
controller and model again
Then my answer would be NO if you was using CGridview's pagination. In your situation, you have to make the pagination by yourself instead. You have already selected all of records, and would like to manipulate them on your client side, you really don't need the pagination of CGridview at all.
Pushing all of records into a page on first load is not good idea, but maybe your requirement has asked, I just say that.

dashboard timeline

I'm trying to implement a dashboard similar to facebook in cakephp (getting posts and post them to timeline and while you press see more it keeps retrieving posts from previous offsets) , but im still confused about the logic and tools , should i use the cakephp pagination class in my implementations.
$this->paginate();
it somehow should be called through ajax accourding to some performance wise
Any helps or suggestions where to start from ?
Thanks All
Don't use paginate
If you paginate something that you are prepending data to - you're going to get data overlapping such that you ask for page 2 - and get the end of, as far as the current user is concerned, the previous page.
Use a timestamp
The normal technique for an endless stream of data is to use a query like:
SELECT *
FROM foos
WHERE created >= $previousLastTimestamp
ORDER BY created DESC
LIMIT 20
Note that while I'm using created in this example - it can be any field that is pseudo unique.
When you first render the page, store the timestamp of the last entry in a javascript variable, then your "get more posts" logic should be:
Make an ajax (get) request, passing the last timestamp
Perform the above sql query (as a $this->Foo->find call)
in your js update the last timestamp so that you know where you are up to for the next time the user clicks "get more posts"
The reason to use a >= condition is that, unless the field you are testing against has unique values, it's possible for there to be multiple rows with the value you're testing for. If you have a naturally-unique field that you are sorting by (id) then you don't need to use greater-or-equal, you can simply use greater-than, and avoid needing to think about duplicate rows.
Here's a reference which explains in more detail why you should handle systems like this avoiding traditional pagination.

Resources