在 LARAVEL 5.8 中对整数调用成员函数 notify()

Call to a member function notify() on integer in LARAVEL 5.8

我正在制定任务计划。流程是我需要发送通知作为他们截止日期的提醒。

这是我的 console\command\remindDuedate

class remindDuedate extends Command
{
    protected $signature = 'remindDuedate:run';   
    protected $description = 'Command description';    
    public function __construct()
    {
        parent::__construct();
    }    
    public function handle()
    {
        while (true) {
            $loanapplications = LoanApplication::where('archive',false)->where('status','=',2)->get();          

            foreach ($loanapplications as $application) {

                $user = $application->user_id;

                $date_approval = Carbon::createFromTimestamp(strtotime($application->date_approval));

                $duration = $application->loanDuration->num_days;
                $duedate_warning = $duration-3;

                $reminder_date = $date_approval->addDays($duedate_warning)->toDateString();

                $now = Carbon::now('Asia/Manila')->toDateString();
                $duedate = Carbon::now('Asia/Manila')->addDays(3)->toDateString();

                if($reminder_date == $now) {

                    $user->notify(new remindDuedateNotif());
                }
            }           
        }
    }
}

php artisan remindDuedate:run

remindDuedateNotif

为什么我得到 "Call to a member function notify() on integer"

提前致谢!

你没有获取用户,因此用户仍然是一个整数,这样设置。

$user = User::find($application->user_id);

编辑

如您所见,您的通知将用户作为第一个参数。所以一并寄出。

$user->notify(new remindDuedateNotif($user));

在用户对象上发送和通知并传递它很奇怪。你很幸运,因为每个 $notifiable 参数实际上都是一个用户,因为它是你发送它的对象。

因此从 __contruct() 中删除 $user 并且在任何访问该用户的地方,您都可以执行以下操作。

'user_id' => $notifiable->id,

1) 用户模型应该具有通知特征

Illuminate\Notifications\Notifiable

2) 您需要在应用程序模型中添加应用程序和用户之间的关系

   public function user()
    {
        return $this->belongsTo(User::class);
    }

3) 通知应用程序用户:

$application->user->notify(new remindDuedateNotif());