Reysa's avatar
Level 1

sort data with extra key and paginate

Hello , sorry if title is so confusing , my English is not that good and I really couldn't find a better title xD This is my code:

$data = User::whereJsonContains('role', 'advertiser')->paginate(20);
foreach ($data as $v) {
  $v['cut'] = $v->cut($name,$v->role,$from,$to);
}

so I'm adding new key and value to each of my data and I'm creating a table in view with foreach

now I need to sort my data base of new key cut and I have no idea how can I do it because I'm using pagination and I don't have access to data array after pagination

0 likes
9 replies
Sinnbeck's avatar

What is the cut, and could it be done in the database?

Reysa's avatar
Level 1

@Sinnbeck it's a booking system and I'm calculting people cut for admin panel and no I can't add that to db , I'm calculting it in a function

Reysa's avatar
Level 1

@Tray2 yes I know but it's not a simple calculation , their cuts depends on their roles (6 different role) and what they sold (6 different product) and it needs to be live update , so if I do that I need to update the field on every sell and cut is not the only field that I have , I have separated calculation for each type of product and the sum of them so that's gonna be too many queries at same time and it could cause high loading time we also have ~ 200 people creatung/editing/deleting selles so it's too many xD

Reysa's avatar
Level 1

ah forgot to mention , I need to have cuts filter by date so adding it to db is bot gonna work

Sinnbeck's avatar

If I understand you correctly, you want to redo the pagination based on ALL the rows in the database, as the order might have changed based on cut?

So maybe set a column to the value? You could have a scheduled job that calculates it every 10 minutes and updates the database.

Reysa's avatar
Level 1

@Sinnbeck I can't add it on db at all , I need to have cuts base of date , for example I want to see cuts from 01/05 to 01/12 , or another date so I need to calculate cuts of their sells during that time , so if I add it on db I can't have that I have a list of users , I'm calculating their cuts with a function called cut() , now I want to sort my table base of higher to lower cut not their user id or anything else and paginate the data

Sinnbeck's avatar

@alirezatig3r But you want to sort it by the cut and then paginate right?

If so, then you need to extract every single row from your database, calculate cuts, and then manually paginate them. If you have alot of rows in the database then this will of course be extremly slow and require alot of memory

Please or to participate in this conversation.