数据库中的Laravel 5.5 Notification Set Null

时间:2019-04-30 19:20:50

标签: laravel notifications laravel-5.5

我正在尝试为用户设置通知。但是,当我尝试在我的RequestinfoController中将数据设置到数据库中时,它总是发送null:user_id具有值

Xampp PHP版本7.2.5 阿帕奇/2.4.33 Laravel 5.5

/* Controller */
 public function acceptRequest($employer_id, $user_id)
  {
      $requestinfo = Requestinfo::where(['employer_id'=> $employer_id, 'user_id'=>$user_id])->first();
      $employer = Employer::find($employer_id);

          $requestinfo->update(['accepted'=> 1]);
          if($requestinfo){
            $employer->notify(new AcceptedRequest($user_id));

            return alert_msg('success', 'update_success' ,'requestinfo');
          }
      return alert_msg('error', 'update_error' ,'requestinfo');
  }

这是通知代码

/* Notification */
class AcceptedRequest extends Notification
{
    use Queueable;
    public $user_id;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user_id)
    {
        $this->$user_id = $user_id;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            "user_id"=> $this->user_id,
            "message"=> "request info accepted",
            "icon"=> "<i class='fas fa-check-square'></i>"
        ];
    }
}

//数据库中的实际结果

{“ user_id”:空,“消息”:“接受请求信息”,“ icon”:“ ”}

1 个答案:

答案 0 :(得分:0)

默认情况下,初始化对象时,变量设置为null。这就是为什么您将null作为user_id的原因。真正的问题在于构造方法-

 public function __construct($user_id)
    {
        $this->$user_id = $user_id;
    }

我们不访问$之前的对象变量,您需要将其更改为$this->user_id = $user_id;

$this->$user_id基本上是实例化 Double Variable ,这意味着$user_id的值成为变量。

因此,如果$user_id的值为10,则$this->$user_id的结果为$this->10,这没有任何意义。

但是,如果将值设为dummy_value,则$this->$user_id的结果为$this->dummy_value,并且可能有一个名为dummy_user的对象变量。

如果使用得当,Double变量是一个非常强大的功能。