Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lquintana's avatar

Append values to pagination

I've being following Laracast for so long time and I am finally posting my first question.

I am working on a very dirty project, pretty much a mess and I am very limited with the changes that I can do.

I need to query students with exams and school and return the results in a pagination. It's pretty much simple up to here, but my problem is that each of the students have a field called flag and the flag for each of the students is a number from 1 to 3 it can also be null and in that case it will be taken as the number 1.

I need to count how many students have flag = 1 or null and also for all of the others meaning how many student have flag 2, how many flag 3. This need to happen before paginate since I need the total for the entire results and not only for the current page.

I have to add this value as property of the pagination since I still need to return all of the students in the same query or I would not mind to $appends flag_1, flag_2, flag_3 to each of the students and I could extract the value from the first student in that case.

This is pretty much what I have.

​
$exam = Exam::find(1);
$students = $exam->students->with('exam','school')->where('end_time', '<=', Carbon::now())

$flagOne = I need to find the students with flag = 1 or null;
$flagTwo = I need to find the students with flag = 2;
$flagThree = I need to find the students with flag = 3;

$students->paginate(10);

0 likes
6 replies
jlrdw's avatar

There's an example of apending links in the chapter on pagination. It's just an array.

MichalOravec's avatar

You can do normal pagination and there exists ->total(), but it return count all of them. And when you need count by flag just write another select for that like:

$countFlagOne = Student::where('flag', 1)->orWhere('flag', null)->count();

$countFlagTwo = Student::where('flag', 2)->count();

$countFlagThree = Student::where('flag', 3)->count();
lquintana's avatar

@michaloravec Thank you for your answer. That does not work. I tried it before and I did it again now in case I was doing something wrong. I have some conditions apply to the students before and applying this way it only add more conditions to the query ending in 0 results.

​```

$exam = Exam::find(1); $students = $exam->students()->where('end_time', '<=', Carbon::now());

$countFlagOne = $students->where('flag', 1)->orWhere('flag', null)->count();

$countFlagTwo = $students->where('flag', 2)->count();

$countFlagThree = $students->where('flag', 3)->count();

​```

wingly's avatar
wingly
Best Answer
Level 29

if you want to do it like that you can try this for example:

$query  = $exam->students()->where('end_time', '<=', Carbon::now());
$countFlagOne = with(clone $query)->where('flag', 1)->orWhere('flag', null)->count();
$countFlagTwo = with(clone $query)->where('flag', 2)->count();
$countFlagThree = with(clone $query)->where('flag', 3)->count();
Snapey's avatar

Why can you not just pass seperate variables to the view? Why does it need to be mixed up in pagination?

lquintana's avatar

@wingly that's exactly what I needed. I am pretty sure that I can do it better with other options, but this variables were being setup looping each of the students using the ini_set(memory) and they are all over the controller, so your way can keep the name of the variables in a much cleaner way. Thank you!

Please or to participate in this conversation.