$assignees = $this->assignees()->get();
Notification::send($assignees, new DataChangeEmailNotification($payload));
or
$this->loadMissing('assignees');
Notification::send($this->assignees, new DataChangeEmailNotification($payload));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to send emails to the assignees + admins for every action Here is my User Model
<?php
namespace App\Models;
use App\Models\UserAlert;
use App\Support\HasAdvancedFilter;
use App\Traits\HasTeam;
use Carbon\Carbon;
use DateTimeInterface;
use Hash;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements HasLocalePreference, MustVerifyEmail
{
use HasFactory, HasAdvancedFilter, Notifiable, HasTeam, SoftDeletes;
public $table = 'users';
protected $casts = [
'is_approved' => 'boolean',
];
protected $hidden = [
'remember_token',
'password',
];
protected $dates = [
'email_verified_at',
'created_at',
'updated_at',
'deleted_at',
];
protected $fillable = [
'name',
'email',
'password',
'locale',
'team_id',
'is_approved',
];
public $orderable = [
'id',
'name',
'email',
'email_verified_at',
'locale',
'team.name',
'is_approved',
];
public $filterable = [
'id',
'name',
'email',
'email_verified_at',
'roles.title',
'locale',
'team.name',
];
public function getIsAdminAttribute()
{
return $this->roles()->where('title', 'Admin')->exists();
}
public function scopeAdmins()
{
return $this->whereHas('roles', fn ($q) => $q->where('title', 'Admin'));
}
public function alerts()
{
return $this->belongsToMany(UserAlert::class)->withPivot('seen_at');
}
public function preferredLocale()
{
return $this->locale;
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
public function getEmailVerifiedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function setEmailVerifiedAtAttribute($value)
{
$this->attributes['email_verified_at'] = $value ? Carbon::createFromFormat(config('project.datetime_format'), $value)->format('Y-m-d H:i:s') : null;
}
public function setPasswordAttribute($input)
{
if ($input) {
$this->attributes['password'] = Hash::needsRehash($input) ? Hash::make($input) : $input;
}
}
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function getCreatedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function getUpdatedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function getDeletedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function team()
{
return $this->belongsTo(Team::class);
}
}
Here is my Project Model :-
<?php
namespace App\Models;
use App\Support\HasAdvancedFilter;
use App\Traits\Auditable;
use App\Traits\Tenantable;
use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Project extends Model
{
use HasFactory, HasAdvancedFilter, SoftDeletes, Tenantable, Auditable;
public $table = 'projects';
public const STATUES_RADIO = [
'active' => 'Active',
'hold' => 'Hold',
'closed' => 'Closed',
];
protected $dates = [
'created_at',
'start_date',
'end_date',
'updated_at',
'deleted_at',
];
protected $fillable = [
'name',
'owner_id',
'start_date',
'end_date',
'statues',
'team_id',
];
public $orderable = [
'id',
'name',
'owner.name',
'created_at',
'start_date',
'end_date',
'statues',
'updated_at',
'deleted_at',
'team.name',
];
public $filterable = [
'id',
'name',
'owner.name',
'created_at',
'package.name',
'start_date',
'end_date',
'statues',
'assignee.email',
'updated_at',
'deleted_at',
'team.name',
];
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
public function owner()
{
return $this->belongsTo(User::class);
}
public function getCreatedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function package()
{
return $this->belongsToMany(Package::class);
}
public function getStartDateAttribute($value)
{
return $value ? Carbon::parse($value)->format(config('project.date_format')) : null;
}
public function setStartDateAttribute($value)
{
$this->attributes['start_date'] = $value ? Carbon::createFromFormat(config('project.date_format'), $value)->format('Y-m-d') : null;
}
public function getEndDateAttribute($value)
{
return $value ? Carbon::parse($value)->format(config('project.date_format')) : null;
}
public function setEndDateAttribute($value)
{
$this->attributes['end_date'] = $value ? Carbon::createFromFormat(config('project.date_format'), $value)->format('Y-m-d') : null;
}
public function getStatuesLabelAttribute($value)
{
return static::STATUES_RADIO[$this->statues] ?? null;
}
public function assignee()
{
return $this->belongsToMany(User::class);
}
public function getUpdatedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function getDeletedAtAttribute($value)
{
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.datetime_format')) : null;
}
public function team()
{
return $this->belongsTo(Team::class);
}
}
Here is my ProjectObserver :-
<?php
namespace App\Observers;
use App\Models\Project;
use App\Models\User;
use App\Notifications\DataChangeEmailNotification;
use Notification;
class ProjectObserver
{
public function created(Project $project): void
{
$payload = [
'action' => 'created',
'model' => sprintf('%s#%s', get_class($project), $project->id),
'reason' => auth()->user(),
];
$admins = User::admins()->get();
Notification::send($admins, new DataChangeEmailNotification($payload));
}
public function updated(Project $project): void
{
$payload = [
'action' => 'updated',
'model' => sprintf('%s#%s', get_class($project), $project->id),
'reason' => auth()->user(),
];
$admins = User::admins()->get();
Notification::send($admins, new DataChangeEmailNotification($payload));
}
public function deleted(Project $project): void
{
$payload = [
'action' => 'deleted',
'model' => sprintf('%s#%s', get_class($project), $project->id),
'reason' => auth()->user(),
];
$admins = User::admins()->get();
Notification::send($admins, new DataChangeEmailNotification($payload));
}
}
here my migration :-
Schema::create('project_user', function (Blueprint $table) {
$table->unsignedBigInteger('project_id');
$table->foreign('project_id', 'project_id_fk_9107252')->references('id')->on('projects')->onDelete('cascade');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id', 'user_id_fk_9107252')->references('id')->on('users')->onDelete('cascade');
});
How to send emails to Admin + Assignee ???
Please or to participate in this conversation.