try
$batsmens = $tag->batsmen->sortByDesc(function ($item) { return $item->reviews->max('rating'); });
@foreach($batsmens as $batsmen)
@endforeach
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a tagging system on my site and clicking on a tag will take you to a list associated of items associated with that tag. I want to order these items(batsmen) on the highest avg 'rating' score they have been given by users who have reviewed them, so the top rated batsmen associated with the tag shows first. How would i do this?
My tables:
Currently i am just finding all on tag($id) in my Tag controller and using a foreach with the relationship to bring back batsmen related to that tag, for example
@foreach($tag->batsmen as $tags)
@endforeach
Please or to participate in this conversation.