public function updateAndQueue($subject, $body, $notificationType, $action) { // Get the emails that are applicable to this subscription and update them Email::where('status', 'Held')->whereHas('notification.notificationType', function ($q) use($notificationType, $body, $subject) { $q->where('name', $notificationType); })->update(array('content' => $subject, 'body' => $body, 'status' => $action)); // Get the email subscriptions which do not have a corresponding email and create them $notificationsWithNoCorrespondingHeldEmail = Notification::whereHas('notificationType', function ($q) use($notificationType) { $q->where('name', $notificationType); })->whereDoesntHave('emails', function ($q) { $q->where('status', '=', EmailStatus::Held); })->get(); $notificationsWithNoCorrespondingHeldEmail->emails()->save(new \Email(array('subject' => $subject, 'body' => $body, 'status' => $action))); }
private function sendEmails($users, $subject, $view, $data, $notificationType) { $userEmails = array_pluck($users->toArray(), 'email'); Mail::send($view, $data, function ($message) use($userEmails, $subject) { $message->to($userEmails)->subject($subject); }); $now = Carbon::now(); $emails = $users->foreach(function ($user) use($now, $subject, $data, $view, $notificationType) { $email = new Email(); $email->user_id = $user->user_id; $email->notification_id = $notificationType; $email->subject = $subject; $email->body = view($view)->with($data)->render(); $email->status = EmailStatus::Sent; $email->sent_at = $email->created_at = $email->updated_at = $now; }); Email::insert($emails); }