If I understand you correctly, you want to sort the users inside $data['cevaplayanlar'] by their score?
$sortedUsers = collect($data['cevaplayanlar'])->sortBy(function ($user) use ($data) {
return $data['quiz']->getUserScore($user->id);
})
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I have a loop like this;
foreach($data['cevaplayanlar'] as $user)
<div class="flex justify-between my-1">
<p>{{ $user->name }}</p><p>Puan: {{ $data['quiz']->getUserScore($user->id) }}</p>
</div>
endforeach
I have a user collection in $data['cevaplayanlar'] And I want to loop through this collection[.] For every data, I get that users ID and calculate his score with fuction getUserScore based on the ID of the quiz[.] But that function needs the ID of the user[.] So I can't use it outside of loop[.]
So there is a function that uses a function from Quiz model (getUserScore(userid)) I want to sort the data by getUserScore return value Is it possible? How can I do that?
If I understand you correctly, you want to sort the users inside $data['cevaplayanlar'] by their score?
$sortedUsers = collect($data['cevaplayanlar'])->sortBy(function ($user) use ($data) {
return $data['quiz']->getUserScore($user->id);
})
Please or to participate in this conversation.