I have set up my website to send a notification to the owner of a post when another user comments on the post saying '"name of the user" commented on your post'
I need to be able to delete this notification if the user that made the comment deleted the post as if it is not deleted the other user can still click on the notification and then then get an error because the post no longer exists same with the comment if a user deletes the comment the notification should be deleted.
this is how i create a comment and send the notification in my 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');
}
So i think that i have to delete the notification in the destroy function but i'm not sure how to do this.
this is my destroy function:
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');
}