ErrorException 未定义变量 $data

时间:2021-02-12 12:28:27

标签: laravel

在数据库通知中,我想向特定用户发送通知

但是在构造函数中,$data 显示为 undefined

这里Employee是我的模型名,我想通知employee表中的employee id 1,数据将被插入到employee中

$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
        ];
    }
}

1 个答案:

答案 0 :(得分:0)

你的问题是因为你在构造函数参数中写了 $employee 但是你使用了数据,所以数据不存在,把它改成这样:-

   public function __construct(Employee $employee)
    {
       
        $this->val=$employee;
    }
相关问题