首页 > 解决方案 > 调用 int 上的成员函数 notify()

问题描述

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

public function toArray($notifiable)
{
    return [
        //
    ];
}
}

当我提交时,我收到了这个错误:

调用 int 上的成员函数 notify()

并且控制器中的这段代码被突出显示:

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

看图片

通知错误

我该如何解决?

标签: laravel

解决方案


$unapproved_post您正在对变量调用 notify 函数,这是int因为您将update查询值存储在其中,如下所示:

$unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',0)
            ->update([
                'is_published' => 1,
                'is_approved' => 1

                ]);

推荐阅读