vincent15000's avatar

Paginate and position the cursor on the last page

Hello,

I need to paginate an eloquent result and position immediately the cursor on the last page.

How is it possible to do that ?

I have tried something with the cursors, but nothing works.

Any help ?

Thanks a lot.

Vincent

0 likes
17 replies
LaryAI's avatar
Level 58

You can use the forPage method on the Eloquent query builder to paginate the results and position the cursor on the last page. The forPage method takes two parameters: the page number and the number of results per page. To position the cursor on the last page, you can use the lastPage method to get the total number of pages and then pass that as the first parameter to the forPage method.

For example:

$results = Model::query()->forPage(Model::query()->lastPage(), 10)->get();

This will return the last page of results with 10 results per page.

1 like
vincent15000's avatar

@LaryAI Ok but this will work only if I need every time the last page.

In fact I need to handle pagination, but on first load the last page instead of the first page.

vincent15000's avatar

@jlrdw @mohamedtammam I was searching for a way to use ->paginate() and setting manually the page parameter passed by the pagination system on the last page number.

But to do this I have to know how many pages there are and this is not possible before querying the database.

MohamedTammam's avatar

@vincent15000 Yes, you're right. You need to do an extra query for that.

$count = YourModel::count();
$results = YourModel::paginate(floor($count / $numberOfRecordsPerPage));
2 likes
jlrdw's avatar

@vincent15000 you need a count of pages.

Edit:

With regular paginate, you can just go to the last page.

2 likes
Snapey's avatar

what do you mean by 'cursor'?

1 like
Snapey's avatar

if no page number is specified in the request, what happens if you first set the page number to an impossibly high balue?

2 likes
vincent15000's avatar

@Snapey I already had the case with Livewire, if I have an page number higher than the max page number, the retrieved collection is empty.

jlrdw's avatar

@vincent15000 how many results? But again cursor does not have a page count. But try regular paginate, there you can go directly to the last page.

Furthermore and curious, why do you need the last page? Just order desc and get first page.

2 likes
vincent15000's avatar

@jlrdw What do you name regular paginate to be able to go directly to the last page ?

Hmmm ... I want to order by date ascending, but retrieve the last page. If I order descending, I have the dates ordered descending and not ascending.

Snapey's avatar

looks like you will need two queries

only run the extra query when $request->page is unset

bear in mind that you might get a full page of results but equally you might get a single entry

2 likes
vincent15000's avatar

@Snapey That's true, I didn't thought about it, effectively I might get a single entry. What would be better is to retrieve the last 10 results for example or perhaps the results for the last month.

It's for a personal project to learn how to manage float number, so I'm trying to develop a very very simple accounting application to store only the incomes, expenses and fees without any other feature.

And the aim is to display all transactions ordered by date ascending, but on the first page load directly see the last page.

1 like
Snapey's avatar

@vincent15000 a strong tip. never handle money with floats. its imprecise and leads to rounding issues, particularly when calculating percentages

decide on the lowest denomination that suits your app and use that. For most shopping carts this might be cents. Multiply the data up when displaying. So something costing $11.75 would be held as integer value 1175

Occasionally, in some industries where things are bought in large volumes , the price per unit might be $0.075 so the system would then need to be all in 10ths of a cent

2 likes
vincent15000's avatar

@Snapey I don't handle money with floats, I use integers. I convert integers to floats only to display the amounts in the views. But in my mind, when I said that I need to handle floats, for me storing floats as integers is a way to handle floats efficiently to not have problems with rounding.

jlrdw's avatar

@vincent15000

or perhaps the results for the last month.

You can write a report query to get a month of data.

1 like

Please or to participate in this conversation.