public static function createOrUpdateVisit(array $data, $visit) { $stat = TrainingVisit::where('training_id', $data['training_id'])->where('player_id', $data['player_id'])->first(); if (!$stat) { if ($visit) { TrainingVisit::create(['training_id' => $data['training_id'], 'player_id' => $data['player_id'], 'visit' => $visit]); } } else { if ($visit) { $stat->visit = $visit; $stat->save(); } else { $stat->delete(); } } }
/** * Handle the event. * * @param TrainingVisitChange $event * @return void */ public function handle(TrainingVisitChange $event) { $player = Player::find($event->playerId); $training = Training::find($event->trainingId); $users = \App\Models\User::whereNotNull('email')->where('email', '!=', '')->get(); $status = $event->visit; $visitList = TrainingVisit::getVisitList(); $subject = trans('email.training.subject') . ' ' . str_limit($player->name, 10) . ' ' . str_limit($training->name, 10) . ' ' . $visitList[$status]; Mail::send('emails.training_visit', ['player' => $player, 'training' => $training, 'status' => $status], function ($m) use($users, $subject) { foreach ($users as $user) { if ($user->isAdmin()) { $m->to($user->email); } } $m->subject($subject); }); }