【发布时间】:2021-01-27 01:45:07
【问题描述】:
我有 2 个表 cmets 和相关回复如下:
回复.php
public function user(){return $this->belongsTo('App\User');}
public function comment(){return $this->belongsTo('App\Comment');}
comment.php
public function user(){return $this->belongsTo('App\User');}
public function replies(){return $this->hasMany('App\Reply');}
- 回复表有:comment_id |用户 ID
- cmets 表有:user_id
我正在尝试创建通知..当用户回复特定评论时..评论所有者会收到通知。
所以我创建了一个名为 RepliedToComment.php 的新通知
public function __construct($reply)
{
$this->reply=$reply;
}
public function via($notifiable)
{
return ['mail','database'];
}
public function toDatabase($notifiable)
{
return [
'repliedTime'=>Carbon::now(),
'reply'=>$this->reply,
'user'=>$notifiable
];
}
在我的 RepliesController.php 中:
public function store(Request $request, Reply $reply)
{
Reply::create($request->all());
auth()->user()->notify(new RepliedToComment($reply));
return redirect()->back()->with('success', 'Reply Submitted Successfuly');
}
Notifications 现在正在工作,但正如您所见,“回复所有者”是函数中定义的被通知的对象。我试图将 Auth::user 更改为 $reply->comment->user 之类的东西,但这给了我错误..
ErrorException
Trying to get property 'user' of non-object
如何定义要通知的评论所有者而不是授权用户?
【问题讨论】: