Я занимаюсь этим вопросом уже несколько дней. Используя Laravel -5.8, я создал эту таблицу уведомлений:
CREATE TABLE `notifications` (
`id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
`sent_to` int(11) DEFAULT NULL,
`send_by` int(11) DEFAULT NULL,
`subject` varchar(75) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`notification_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'goal setting, goal approval, goal rejecttion',
`data` text COLLATE utf8mb4_unicode_ci NOT NULL,
`is_read` tinyint(4) DEFAULT 0,
`read_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4
Я хочу отправлять уведомления пользователям и сохранять их в базе данных.
NotificationController
public function notify_users(){
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$userId = Auth::user()->id;
$userEmail = Auth::user()->email;
$userCode = Auth::user()->employee_code;
$userFirstName = Auth::user()->first_name;
$userLastName = Auth::user()->last_name;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();
$reviewperiods = DB::table('appraisal_identity')->select('appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
$reviewperiod = $reviewperiods->appraisal_name;
$linemanager = DB::table('hr_employees')->where('id', $userEmployee)->first();
$linemanageremails = DB::table('hr_employees')->select('email')->where('line_manager_id', $linemanager->line_manager_id)->first();
$linemanageremail = $linemanageremails->email;
$linemanagerfirstnames = DB::table('hr_employees')->select('first_name')->where('line_manager_id', $linemanager->line_manager_id)->first();
$linemanagerfirstname = $linemanagerfirstnames->first_name;
$linemanagerlastnames = DB::table('hr_employees')->select('last_name')->where('line_manager_id', $linemanager->line_manager_id)->first();
$linemanagerlastname = $linemanagerlastnames->last_name;
$linemanagerids = DB::table('hr_employees')->select('id')->where('line_manager_id', $linemanager->line_manager_id)->first();
$linemanagerid = $linemanagerids->id;
$unapproved_count = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_approved',0)->count();
if ($unapproved_count > 3){
$unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_approved',0)
->update([
'is_approved' => 1
]);
$unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_approved',1)->first();
$details = [
'sent_to' => $linemanagerid,
'sent_by' => $userId,
'subject' => 'Approved by: ' .$userCode .'for '.$reviewperiod,
'greeting' => 'Hello, '.$linemanagerfirstname . ' '. $linemanagerlastname . '!',
'body' => 'The employee with the code: ' . $userCode . 'and Fullname: ' .$userFirstName. ' ' .$userLastName .' ' .'has approved his/her goals for the Review Period: ' .$reviewperiod . ' '. 'for your approval.',
'line1' => 'The employee with the code: ' . $userCode . 'and Fullname: ' .$userFirstName. ' ' .$userLastName,
'line2' => 'has approved his/her goals for the Review Period: ' .$reviewperiod,
'line3' => 'for your approval.',
'thanks' => 'Thank you!',
'user_fullname' => $userFirstName. ' ' . $userLastName,
'user_code' => $userCode,
'user_email' => $userEmail,
'user_id' => $userId,
'line_manager_full_name' => $linemanagerfirstname. ' ' . $linemanagerlastname,
'review_periond' => $reviewperiod,
'line_manager_email' => $linemanageremail,
'line_manager_id' => $linemanagerid,
'notification_type' => 'goal setting',
];
Notification::route('mail', $details['line_manager_email'])
->notify(new \App\Notifications\Appraisal\AppraisalGoalApprove($details));
Session::flash('success', 'Approved successfully');
return redirect()->back();
}else{
Session::flash('info', 'Approval Failed!');
return redirect()->back();
}
}
Как я уже говорил ранее. Я хочу отправить уведомление по электронной почте, а также сохранить в базе данных.
Уведомления
class AppraisalGoalApprove extends Notification implements ShouldQueue
{
use Queueable;
private $details;
public function __construct($details)
{
$this->details = $details;
}
public function via($notifiable)
{
return ['mail','database'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject($this->details['subject'])
->greeting($this->details['greeting'])
->line($this->details['line1'])
->line($this->details['line2'])
->line($this->details['line3'])
->line($this->details['thanks']);
}
public function toDatabase($notifiable)
{
return [
'subject' => $this->details['subject'],
'sent_to' => $this->details['sent_to'],
'sent_by' => $this->details['sent_by'],
'notification_type' => $this->details['notification_type'],
'data' => $this->details['body']
];
}
public function toArray($notifiable)
{
return [
//
];
}
}
Когда я отправил форму, уведомление было отправлено пользователю, но оно не было сохранено в база данных. Я получил эту ошибку:
Вызов функции-члена create () со значением NULL
Эта строка кода подчеркнута:
class DatabaseChannel
{
public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database', $notification)->create(
$this->buildPayload($notifiable, $notification)
);
}
}
Как мне устранить эту ошибку?
Спасибо.