I have got three models
Contacts Model
ContactRoles Model
Project Model
Set-up I have
contacts can belong to multiple projects. (Many to Many)
each project has its own contact roles created by user. (One to Many
Roles are specific to projects)
Within selected project, a contact can be assigned to multiple roles (Many to many).
I am currently building my blade view files and have an issue with loading the data with foreach loop and an if statement.
How I retrieve data in controller;
$project = Project::with('contacts.contactrole','companies.companyrole')->findOrFail($projectid);
Question 1:
@foreach ($project->contacts as $contact)
@foreach ($contact->contactrole as $role)
@if ($role->group == 'consultant')
<li> {{$contact->firstname}} - {{$role->label}} </li>
@endif
@endforeach
@endforeach
Above code works, when a contact has multiple roles, since i iterate through role, listing is repeating it self. How can i constrain inner foreach so that I can iterate through contacts only with "group" condition?