I'm trying to make my notifications for Laravel 6 mail out effectively. I have a model (Event) that has followers (User) as a relationship - see below.
Event: $this->belongsToMany('App\User')
When an action happens, I'd like to trigger an email notification out to all the followers and a few other users. This means I'm trying to combine all the users into a single collection. My current approach is to push any individual users onto the followers collection, then remove any unwanted users based on some logic, and return a deduplicated collection that gets called....
Notification::send($users, new MemoryCreated($memory));
I'm struggling to managing the collection (removing by model (not key since it's an incremental integer) then my effort to do this $users->unique('App\User');
didn't work.
Am I approaching this correctly? How can I modify the collection helpers to use the model->id for the forget and unique methods?
Thanks!
OK I figured it out for anyone interested.
remove all duplicates, e.g. remove owner if already following
$users = $users->unique('id');
if current user is following remove them
$users = $users->reject(function ($value, $key) { return $value->id == Auth::user()->id; });
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community