【问题标题】:Laravel - Notifications to both email and databaseLaravel - 电子邮件和数据库的通知
【发布时间】:2020-02-27 09:57:50
【问题描述】:

在我的 Web 应用程序项目中使用 Laravel-5.8,我有这个 Notification 类:

namespace App\Notifications;

use http\Url;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

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为接收者,邮件内容为数据。

我如何完成我的 AppraisalGoalPublish 和控制器以实现这些目标?

谢谢。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您可以按照以下步骤进行操作 -

    1. 在您的 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 方法一样传递。

    【讨论】:

      【解决方案2】:

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

      $details = [
      
          'full_name' => $FirstName. ' ' . $LastName
      ];
      
      $user->notify(new \App\Notifications\AppraisalGoalPublish($details));
      

      还修改您的 AppraisalGoalPublish() 如下。

      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-notifications

      【讨论】:

        猜你喜欢
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        相关资源
        最近更新 更多