我已将我的网站设置为在其他用户评论帖子时向帖子所有者发送通知说“用户名”评论了您的帖子
如果发表评论的用户删除了帖子,就好像它没有被删除一样,我需要能够删除此通知,其他用户仍然可以单击通知然后收到错误,因为帖子不再存在与评论相同如果用户删除评论,则应删除通知。
这就是我创建评论并在我的 CommentsController 中发送通知的方式:
public function store(Request $request, $post_id)
{
$this->validate($request, array(
'comment' => 'required|min:2|max:2000',
'email' => 'required',
'name' => 'required'
));
$post = Post::find($post_id);
$user = Auth::user();
$comment = new Comment();
$comment->name = $request->name;
$comment->email = $request->email;
$comment->comment = $request->comment;
$comment->approved = true;
$comment->post_id = $post->id;
$comment->user_id = $user->id;
$comment->post()->associate($post);
$comment->save();
User::find($post->user_id)->notify(new CommentCreated($user, $post, $comment));
return redirect()->back()->with('success','Comment Created');
}
所以我认为我必须删除销毁函数中的通知,但我不知道该怎么做。
这是我的销毁功能:
public function destroy($id)
{
$comment = Comment::find($id);
if(auth()->user()->id == $comment->user_id || auth()->user()->role == 'Admin') {
$comment->delete();
return redirect()->back()->with('success','Comment Removed');
}
return redirect('/posts')->with('error','Unauthorised Page');
}