Laravel-调用未定义的方法illumed\Notifications\Messages\MailMessage::to()

Laravel-调用未定义的方法illumed\Notifications\Messages\MailMessage::to(),laravel,Laravel,我正在使用Laravel-5.8发送通知: 控制器 public function publish_all_posts(){ $userCompany = Auth::user()->company_id; $userEmployee = Auth::user()->employee_id; $userId = Auth::user()->id; $userEmail = Auth::user()->email; $user

我正在使用Laravel-5.8发送通知:

控制器

public function publish_all_posts(){
    $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_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

                ]);


        $details = [
            'sent_to' => $linemanagerid,
            'sent_by' => $userId,
            'subject' => 'Goal Published by: ' .$userCode .'for '.$reviewperiod,
            'greeting' => 'Hello, '.$linemanagerfirstname . ' '. $linemanagerlastname . '!',
            'body' =>  'The employee with the code: ' . $userCode . 'and Fullname: ' .$userFirstName. ' ' .$userLastName .' ' .'has published his/her goals for the Review Period: ' .$reviewperiod . ' '. 'for your approval.',
            'line1' => 'The employee with the code: ' . $userCode . 'and Fullname: ' .$userFirstName. ' ' .$userLastName,
            'line2' => 'has published 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',

        ];

        $unapproved_post->notify(new \App\Notifications\Appraisal\AppraisalGoalPublish($details));


        Session::flash('success', 'Goals1 Published successfully');
        return redirect()->back();
    }else{
        Session::flash('info', 'You cannot proceed. Kindly Set all Goals before you publish!');
        return redirect()->back();
    } 
}
通知

class AppraisalGoalPublish 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['line'])
            ->line($this->details['thanks'])
            ->to($this->detail['user_email']); 
   }

 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']
  ];
 }   

}
除了通知之外,其他一切都起作用了。当我尝试提交时,出现以下错误:

调用未定义的方法illumb\Notifications\Messages\MailMessage::to()

这一行在toMail中突出显示:

->到($this->detail['user_email'])

我如何解决它


谢谢

您可以在这种情况下使用按需通知

从通知类中删除“to”,并使用此代码发送通知

Notification::route('mail', $details['user_email'])
            ->notify(new \App\Notifications\Appraisal\AppraisalGoalPublish($details));


确保在文件顶部包含通知外观。

错误非常明显
MailMessage
没有
to()
方法。再次阅读文档:。
to()
方法是在从
toMail()
方法返回
Mailable
时使用的。