【发布时间】:2021-02-12 12:28:27
【问题描述】:
在数据库通知中,我想向特定用户发送通知
但在构造函数中,$data显示为未定义
这里Employee是我的模型名,我想通知员工表中的员工id为1,数据将插入到员工中
$data=Employee::create([
'first_name'=>$request->input('first_name'),
'last_name'=>$request->input('last_name'),
'username'=>$request->input('username'),
'email'=>$request->input('email'),
'password'=>$request->input('password'),
'confirm_password'=>$request->input('confirm_password'),
]);
$admin=Employee::find(1);
$admin->notify(new NotifyAdmin($data));
通知类
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Notifiable;
use App\Models\Employee;
class NotifyAdmin extends Notification implements ShouldQueue
{
use Queueable, Notifiable;
private $val;
public function __construct(Employee $employee)
{
$this->val=$data;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'username'=> $this->val->username
];
}
}
【问题讨论】:
-
您的 $data 作为参数传递,但您的构造函数定义了 $employee 参数。因此 $this->val = $employee;应该工作。
-
如果我使用 $employee 它会显示 BadMethodCallException 调用未定义的方法 App\Models\Employee::notify()
标签: laravel