laravel通知未定义的索引:user_id [英] laravel Notification Undefined index: user_id

查看:163
本文介绍了laravel通知未定义的索引:user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要进行按摩通知,我已经进行了通知步骤, 但是给我这个错误 当我执行dd($notifiable);时,我发现了所有数据

I going to do massage notification, I already make the notifications steps, but gives me this error when I do dd($notifiable); I found all data

未定义索引:user_id或 未定义索引:名称

Undefined index: user_id OR Undefined index: name

public function store(Request $request)
    {
        $chating=new chats();
        $chating->chat = $request->input('chat');
        $chating->user_id = Auth::id();
        $chating->employee_id = $request->input('employeeid');
        $chating->save();

        $user_id=$request->input('employeeid');       
        auth()->user()->notify(new SendMassages($user_id));

        return redirect()->back();
    }

数据库通知表汇总数据{"user_id":5,"name":"Ibrahim"}

database notifications table coulmn data {"user_id":5,"name":"Ibrahim"}

型号

    protected $user_id;
    protected $name;
    public function __construct($user_id)
    { 
        $this->user_id = $user_id;
    }
public function via($notifiable)
    {
        return ['database'];
    }
 public function toDatabase($notifiable)
    {
        return [
            'user_id' => $this->user_id,
            'user'=>$notifiable

        ];
    }

查看:

<a href="{{url('chatnow',$notification->data['user_id'])}}">{{ $notification->data['name'] }}</a>

型号:

class SendMassages extends Notification
{
    use Queueable;
    public $user;
    public $user_id;
    public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }
    public function via($notifiable)
    {
        return ['database'];
    }
    public function toDatabase($notifiable)
    {
      // dd($notifiable);
        return [
           'user_id' => $this->user_id,
           'user'=>$notifiable
        ];
    }
}

推荐答案

似乎您的代码中存在封装问题.首先,如果要使用快速且不安全的方法,则必须将模型的属性公开,但如果要使用OOP的方法,则必须编写setter来为您执行此逻辑. 第一种也不安全的方法:

It seems that you have problem with encapsulation in your code. First you have to make the property of your model into public if you want the quick and not-safe way but if you want the OOP way, You must write setters to do that logic for you. First and not-safe approach :

class YourModel {
    public $user_id;

    .
    .
    .
}

OOP方式:

Class {
    public function setUserId(int $userId)
    {
        $this->user_id = $userId;
    }

    .
    .
    .
}

如果您想获得确切的答案,请在此处复制您的控制器类和模型.

if you are willing to get the exact answer please copy your controllerclass and model in here.

这篇关于laravel通知未定义的索引:user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆