1

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');

}
es915
  • 137
  • 3
  • 11
  • Does this answer your question? [Automatically deleting related rows in Laravel (Eloquent ORM)](https://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm) – Peppermintology Dec 29 '20 at 14:09
  • I dont really understand how i would use that relating to my example – es915 Dec 29 '20 at 14:18
  • if there is a foreign key on the table notification which points to the comment id, then you can add the referential action "ON DELETE CASCADE" so that a notification is automaticaly deleted when a comment is deleted. – Shaolin Dec 29 '20 at 14:20
  • Well if you create a new notification like e.g. `CommentDeleted` you can listen to that one and delete all notifications client-side (note I'm really speculating on how your system works here since there's not really enough information to go on) – apokryfos Dec 29 '20 at 14:48

2 Answers2

0

You need to add your logic in destroy function to delete notification related to that post.

Something Like this,

Notification::find('post_id', $post->id)->delete(); // can be single or multiple change according to your logic

So when it will be removed from notification. And for double check you also put this code in find function where user is redirect to that post from notification.

So you need to use findOrFail or use whereHas to check that post is exist or not. If not and notification is still there then delete that notification and then redirect to appropriate page according to your flow.

Maulik Shah
  • 1,040
  • 1
  • 7
  • 17
-1

If you in CommentCreated Track all of $post->id,$comment->id and $user->id in database notification data column you can easily do that

Notification::where('data->comment_id', $comment->id)->delete();

As data column in notifications table is a JSON TYPE ,so you can handle it as object in eloquent.

Abdelhafz
  • 49
  • 1
  • 6