Вы можете сделать это в следующих шагах -
- Добавить следующие черты в вашу модель HrManager , Ref
Незначительные изменения в вашем контроллере:
public function publish_all_posts()
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();
$reviewperiod = DB::table('appraisal_identity')->select('appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
$linemanager = HrManager::where('id', $userEmployee)->first();
$unapproved_count = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',0)->count();
$linemanager->notify(new AppraisalGoalPublish(Auth::user(), $reviewperiod));
if ($unapproved_count > 3){
$unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',0)
->update([
'is_published' => 1,
'is_approved' => 1
]);
Session::flash('success', 'Goals Published successfully');
return redirect()->back();
}else{
Session::flash('info', 'You cannot proceed. Kindly Set all Goals before you publish!');
return redirect()->back();
}
}
А в вашем классе уведомлений -
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AppraisalGoalPublish extends Notification implements ShouldQueue
{
use Queueable;
private $sender;
private $reviewPeriod;
private $name;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($sender, $reviewPeriod)
{
$this->sender = $sender;
$this->reviewPeriod = $reviewPeriod;
$this->name = $this->sender->first_name.' '.$this->sender->last_name;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->view(
'your.view.path', ['name' => $this->name, 'reviewPeriod' => $this->reviewPeriod]
);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
return [
'sender' => $this->sender->id,
'receiver' => $notifiable->id,
'message' => 'The employee with the code $userCode and name ' . $this->name . ' has published his/her goals for the review Period '. $this->reviewPeriod .' for your approval. Thanks'
];
}
}
Создайте представление (шаблон блейда), которое будет содержать стили электронной почты и все. Внутри этого блейда вы получите две переменные $ name и $ reviewPeriod. Если вы хотите режим, вы можете передать как в toMail метод.