$groupedservices = $user->services->groupBy('name');
pass $groupedservices to the view
<ul>
@foreach($groupedServices as $services)
<li>{{ $services->first()->name }} x {{ $services->count() }}</li>
@endforeach
</ul>
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
so i have a many to many relationship between users and services.
i wanna show a list of all services a user have, and the number of that service.
Example:
A user have these services:
They can have more then 1 domain name, so i want to disaplay: Domain name (x numer).
i have tried different things, but can't seem to find the solution.
Right now i have this code that lists all the services:
$user->service()->get()->unique();
which gives me:
$groupedservices = $user->services->groupBy('name');
pass $groupedservices to the view
<ul>
@foreach($groupedServices as $services)
<li>{{ $services->first()->name }} x {{ $services->count() }}</li>
@endforeach
</ul>
Please or to participate in this conversation.