-
app/Observers/TicketActionObserver.php
Open in GitHubuse App\Notifications\DataChangeEmailNotification; use App\Notifications\AssignedTicketNotification; use App\Ticket; use Illuminate\Support\Facades\Notification; class TicketActionObserver { public function created(Ticket $model) { $data = ['action' => 'New ticket has been created!', 'model_name' => 'Ticket', 'ticket' => $model]; $users = \App\User::whereHas('roles', function ($q) { return $q->where('title', 'Admin'); })->get(); Notification::send($users, new DataChangeEmailNotification($data)); } public function updated(Ticket $model) { if($model->isDirty('assigned_to_user_id')) { $user = $model->assigned_to_user; if($user) { Notification::send($user, new AssignedTicketNotification($model)); } } } }
-
app/Ticket.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; class Ticket extends Model implements HasMedia { // public static function boot() { parent::boot(); Ticket::observe(new \App\Observers\TicketActionObserver); // } // }