Laravel - вызов неопределенного метода Illuminate \ Notifications \ Messages \ MailMessage :: to () - PullRequest
0 голосов
/ 27 февраля 2020

Я использую 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']
  ];
 }   

}

Все остальные вещи работали отдельно от Уведомления. При попытке отправки я получил эту ошибку:

Вызов неопределенного метода Illuminate \ Notifications \ Messages \ MailMessage :: to ()

, и эта строка выделена в toMail:

-> до ($ this-> detail ['user_email']);

notification error message

Как мне решить?

Спасибо

1 Ответ

0 голосов
/ 27 февраля 2020

Вы можете использовать Уведомления по требованию для этого случая.

https://laravel.com/docs/5.8/notifications#on -demand-уведомления

Удалите «to» из вашего класса уведомлений и используйте этот код для отправки уведомления.

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

Не забудьте включить фасад уведомлений в верхней части вашего файла.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...