简体   繁体   English

当用户删除与通知相关的评论时,如何删除 laravel 中的通知

[英]how do i delete a notification in laravel when a user deletes the comment related to the notification

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:这就是我创建评论并在我的 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.所以我认为我必须删除销毁 function 中的通知,但我不知道该怎么做。

this is my destroy function:这是我的销毁 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');

}

You need to add your logic in destroy function to delete notification related to that post.您需要在销毁 function 中添加您的逻辑以删除与该帖子相关的通知。

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.为了仔细检查,您还将此代码放入 find function 中,用户从通知重定向到该帖子。

So you need to use findOrFail or use whereHas to check that post is exist or not.因此,您需要使用findOrFail或使用whereHas来检查该帖子是否存在。 If not and notification is still there then delete that notification and then redirect to appropriate page according to your flow.如果没有并且通知仍然存在,则删除该通知,然后根据您的流程重定向到适当的页面。

If you in CommentCreated Track all of $post->id , $comment->id and $user->id in database notification data column you can easily do that如果您在CommentCreated Track all of $post->id , $comment->id$user->id在数据库通知数据列中,您可以轻松地做到这一点

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.由于通知表中的数据列是JSON TYPE,因此您可以将其处理为eloquent中的object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM