Laravel-通知电子邮件和数据库



在我的web应用程序项目中使用Laravel-5.8,我有一个通知类:

namespace AppNotifications;
use httpUrl;
use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
class AppraisalGoalPublish extends Notification implements ShouldQueue
{
use Queueable;
public function __construct()
{
}
public function via($notifiable)
{
return ['mail','database'];
}
public function toMail($notifiable)
{
return (new MailMessage)
}
public function toDatabase()
{
return [
];
}   
public function toArray($notifiable)
{
return [
//
];
}
}

和这个控制器

public function publish_all_posts(){
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;    
$userId = Auth::user()->id;
$userEmail = Auth::user()->id;
$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();
$reviewperiod = DB::table('appraisal_identity')->select('appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
$linemanager = DB::table('hr_employees')->select('line_manager_id')->where('id', $userEmployee)->first();
$linemanageremail = DB::table('hr_employees')->select('email')->where('line_manager_id', $linemanager)->pluck('email');
$linemanagerid = DB::table('hr_employees')->select('id')->where('line_manager_id', $linemanager)->pluck('id');

$unapproved_count = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',0)->count();
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();
} 
}

我想在提交publish_all_posts()时发送邮件通知并保存到数据库中

  1. 我想向$linemanageremail发送邮件通知,通知内容为:代码为$userCode、名称为$FirstName. ' ' . $LastName的员工已发布了他/她的审核期$reviewperiod目标,供您批准。感谢

  2. 将邮件通知的内容保存到通知表中:以$userId为发送方,$linemanagerid为接收方,邮件内容为数据。

如何完成我的评估目标发布和控制器以实现这些目标?

谢谢。

您可以在以下步骤中做到这一点-

  1. 在您的HrManager模型中添加以下特征,参考

控制器发生轻微变化:

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 AppNotifications;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
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 IlluminateNotificationsMessagesMailMessage
*/
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方法。

在重定向用户之前,在控制器操作publish_all_posts((中添加以下行。您可以根据自己的要求更改阵列。

$details = [
'full_name' => $FirstName. ' ' . $LastName
];
$user->notify(new AppNotificationsAppraisalGoalPublish($details));

同时按如下方式修改您的评估目标发布((。

public function toMail($notifiable)
{
return (new MailMessage)
->line($this->details['full_name']);
}
public function toDatabase($notifiable)
{
return [
'data' => $this->details['full_name']
];
}

有关更多详细信息,请参阅文档-https://laravel.com/docs/5.8/notifications#mail-通知

相关内容

最新更新