Laravel - вызов функции-члена create () в null при сохранении уведомления в базу данных - PullRequest
0 голосов
/ 02 марта 2020

Я пытаюсь использовать Laravel -5.8 и sendgrid для отправки уведомления, а также сохранить в базе данных.

Контроллер

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

                ]);

    $unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',1)->first();


    $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',

    ];

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


        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();
     } 
    }

Я изменил табличные уведомления на следующие поля:

id, sent_to (int), send_by (int), subject (varchar), Notification_type (varchar), data (text), read_at.

Я хочу отправить уведомление по электронной почте и также сохраните в базе данных.

Уведомления

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['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)
    );
  }
}

Как мне устранить эту ошибку?

Спасибо.

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