Ad

October CMS - Send Mail To Members Of Backend Group

- 1 answer

I've set up a group for a particular set of admin user's who need to be notified when new content is published. Let's call this group 'notify-admins' for the sake of argument.

I have this code, taken from the October docs on for (Users and Permissions)

Mail::sendTo(UserGroup::where('code', 'notify-admins')->get()->users, 'mailTemplate', $data);

However, this throws a Property [users] does not exist on this collection instance so clearlyI'm doing something wrong!

All I need to do is pop out an email to all members of the group - whats the best way?

Ad

Answer

So, I was doing this the wrong way around. Rather than query UserGroups I needed to look at Users who were members of groups. This is code I ended up using to select the users within the target group and then iterate over them popping out emails.

$admins = User::whereHas('groups', function($query) {
   $query->where('code', 'notify-admins');
})->get();

Then to email...

foreach ($admins as $admin) {
    Mail::sendTo($admin->email, mailTemplate, $data);
}
Ad
source: stackoverflow.com
Ad