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

Brammah's avatar

Send Email to Multiple Users based on Multiple selected roles

Hi Team, I have an issue that I need some assistance with. I have a project form that I am assigning projects to departments, I have used the Spatie Permissions and while assigning the project managers, I am selecting them based on their roles because there are several of them with the same Permission.

My challenge is that when I send the mail, I choose their roles and only the first user with that role gets a notification and the rest don't. Is there someone who can assist kindly?

            <label for="projectOwners" class="text-dark font-weight-bold">Project Owners: <span class="text-danger">*</span></label>
            <select name="projectOwnerRoles[]" class="form-control" id="projectOwners" multiple style="width: 100%;">
                @foreach ($projectOwnerRoles as $projectOwnerRole)
                    <option value="{{ $projectOwnerRole->id }}"  {{ ($project->projectOwnerRoles->contains($projectOwnerRole->id)) ? 'selected' : ''}} >
                        {{$projectOwnerRole->role_name}}
                    </option>
                @endforeach
            </select>
            @error('partners') <span class="invalid-feedback"><strong>{{ $message }}</strong></span> @enderror
        </div>
0 likes
3 replies
Brammah's avatar

Controller bit;

public function store(StoreProjectRequest $request)
      {
        $project->projectOwners()->attach($request->projectOwnerRoles);

        Notification::send($project->projectOwners, new ProjectAssignedNotification($project));

        return to_route('project.show', $project)->with('Project Created Successfully.');
      }
Brammah's avatar

After some research, I finally found a solution. On the controller, while getting the users for create, you get them with the roles relationship.

  ``public function create()
   {
      $projectOwners = User::with('roles')->get();
      return view('project.create', compact('projectOwners'));
    }

Afterward, on the create form, the foraech loop will slightly change:

<div>
      <label for="projectOwners" 
           class="text-dark font-weight-bold">Project Owners: 
           <span class="text-danger">*</span>
      </label>
      <select name="projectOwnerRoles[]" 
              class="form-control" 
              id="projectOwners" 
              multiple 
              style="width: 100%;">
              @foreach ($projectOwnerRoles as $projectOwnerRole)
                  <option value="{{ $projectOwnerRole->id }}"  
                      {{ ($project->projectOwnerRoles->contains($projectOwnerRole->id))
                             ? 'selected' : ''}} >
                           @foreach($projectOwnerRole->getRoleNames() as $role)
                       		    {{ $role->name }}
                           @endforeach
                   </option>
               @endforeach
        </select>
        @error('projectOwners') 
              <span class="invalid-feedback">
					<strong>{{ $message }}</strong>
              </span>
        @enderror
 </div>
Brammah's avatar

since it will display the roles when selecting, on the store method, you can perform the below and it will serve the same purpose:

   public function store(StoreProjectRequest $request)
      {
        $project->projectOwners()->attach($request->projectOwnerRoles);

        Notification::send($project->projectOwners, 
                                new ProjectAssignedNotification($project));

        return to_route('project.show', $project)->with('Project Created Successfully.');
      }

Please or to participate in this conversation.