简体   繁体   English

确定评论的用户并发送通知Laravel

[英]Identify user of comment and send notification Laravel

I have a posts system. 我有一个职位系统。 On my site, users can post articles and post comments on every article. 在我的网站上,用户可以发布文章,并对每篇文章发表评论。 I want to release: when any user (not author of the post) comment post, I need to send a notification to the author of the post, that post him was commented by UserName... I do it like this: 我要发布:当任何用户(不是帖子的作者)发表评论时,我需要向该帖子的作者发送通知,该帖子由UserName进行评论...我这样做是:

In CommentController I have a method for post comment on the post: 在CommentController中,我有一个用于在帖子上发表评论的方法:

public function postComment(Post $post, AddPostRequest $request)
{
    if ($request->parent_id !== null) {
        $parent = Comment::where('id', $request->parent_id)->first();
        $this->authorize('canComment', $parent);
    }

    $data = [
        'user_id' => Auth::check() ? Auth::user()->id : null,
        'post_id' => $post->id
    ];

    $comment = $post->createComment(array_merge($request->except('g-recaptcha-response'), $data));

    $post->owner->sendReviewNotification($review);

    return $review;
}

In my comment system, I have nested comments. 在我的评论系统中,我嵌套了评论。 In model Post, I have the method createReview. 在模型Post中,我具有方法createReview.

public function owner()
{
    return $this->belongsTo(User::class, 'user_id');
}

public function comments()
{
    return $this->hasMany(Comment::class);
}

public function createComment($data = [])
{
    $review = $this->comments()->create($data);

    return $review;
}

In model Comment I have: 在模型评论中,我有:

public function post()
{
    return $this->belongsTo(Post::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

public function parent()
{
    return $this->belongsTo(static::class, 'id', 'parent_id');
}

public function children()
{
    return $this->hasMany(static::class, 'parent_id');
}

public function scopeActive($query)
{
    return $query->whereNotNull('user_id')->where('active', 1);
}

Now: when a comment is created, notifications are sent to the author of the post, but when an author can answer to comment of posting him, then notification send again to him, but I need send a notification to the author of the comment. 现在:创建评论后,通知会发送给帖子的作者,但是当作者可以回答发布他的评论时,通知会再次发送给他,但是我需要向评论作者发送通知。 How can I define this case? 如何定义这种情况? I don't need to send always notifications to an author of a post, I need notify and authors of comments, that comments them is answered by the author of the post. 我不需要总是将通知发送给帖子的作者,我需要通知和评论的作者,这些评论由帖子的作者回答。

From what I understand, you are trying to send and email notification to the author of the post via email class sendReviewNotification with $review as the parameter. 据我了解,您正在尝试通过以$review为参数的电子邮件类sendReviewNotification发送通知并将电子邮件通知给帖子的作者。 if that is the case then this line 如果是这种情况,那么此行

$post->owner->sendReviewNotification($review);

because $post->owner returns an eloquent relationship(It is a query builder) 因为$post->owner返回一个雄辩的关系(这是一个查询生成器)

this is how I would do this. 这就是我要做的。 I would use the relationship above to get the user's(Author) email address 我将使用上面的关系来获取用户的(作者)电子邮件地址

$post->owner->email

the line above will return the Authors email address 上面的行将返回作者的电子邮件地址

then use the mail Facade 然后使用邮件Facade

Mail::to($post->owner->email)->send(new sendReviewNotification($review));

I am assuming sendReviewNotification is your mail class also remember to add this line at the beginning of your class just below the namespace. 我假设sendReviewNotification是您的邮件类,也请记住在类的开头在名称空间下方添加此行。 use Illuminate\\Support\\Facades\\Mail;

I think what you really want is to send a notification to every participant of the discussion, but only to the people that have commented since your last comment: 我认为您真正想要的是向讨论的每个参与者发送通知,但仅向自您上次评论以来发表评论的人发送通知:

Post #4321 (Author Richard)
  -> Comment #1 by John
  -> Comment #2 by Elias
  -> Comment #3 by Richard    <-- the author
  -> Comment #4 by Michael
  -> Comment #5 by John
  -> Comment #6 by Merry
  -> Comment #7 by Richard    <-- the author

In this scenario, you probably want to send a notification in the following sitiations: 在这种情况下,您可能希望在以下情况下发送通知:

  1. Richard is notified if someone commented on his post 如果有人对他的帖子发表了评论,则会通知Richard
  2. The author of a comment is notified if Richard comments as well 如果Richard也发表评论,则会通知评论作者

The first situation seems straight forward. 第一种情况似乎很直接。 Whenever a new comment is created, you notify the author of the post which has been commented: 每当创建新评论时,您都将已评论的帖子通知作者:

if ($comment->author_id !== $comment->post->author_id) {
    $comment->post->author->notify(new PostWasCommented($comment));
}

As you can see, I expect you to use the built-in notification system of Laravel. 如您所见,我希望您使用Laravel的内置通知系统 It makes your life a lot easier to be honest. 说实话,这使您的生活更加轻松。


The second situation is a bit more elaborate and allows again for two possible solutions: 第二种情况较为复杂,并且再次允许两种可能的解决方案:

  • You send a notification to everyone who commented on the post before Richard commented. 您会在Richard发表评论之前向所有对此评论发表评论的人发送通知。
    In the above example, when Richard creates comment #3, John and Elias would receive a notification. 在上面的示例中,当Richard创建注释3时,John和Elias将收到通知。 When Richard creates comment #7, then John, Elias, Michael and Merry receive a notification. 当理查德(Richard)创建第7条评论时,约翰(John),埃里亚斯(Elias),迈克尔(Michael)和迈里(Merry)会收到通知。 ( $comment is the new comment to be created.) $comment是要创建的新注释。)

     $users = User::query() ->whereHas('comment', function ($query) use ($comment) { return $query->where('post_id', $comment->post_id); }) ->where('user_id', '!=', $comment->post->author_id) ->get(); foreach ($users as $user) { $user->notify(new PostWasCommented($comment)); } 
  • You only send a notification to the people that commented since the last comment of the author. 您仅向自作者的最后评论以来发表评论的人员发送通知。
    Projected on the above example, this means that John and Elias would be notified when Richard creates comment #3. 根据上述示例,这意味着当Richard创建注释3时,将通知John和Elias。 And when Richard creates comment #7, then only Michael, John and Merry are notified - but not Elias. 当理查德(Richard)创建第7条评论时,只有迈克尔(Michael),约翰(John)和梅里(Merry)会收到通知,但不会通知埃里亚斯(Elias)。 This is because only these three people have commented between #3 and #7. 这是因为只有这三个人在#3和#7之间发表了评论。

     $users = User::query() ->whereHas('comment', function ($query) use ($comment) { return $query->where('post_id', $comment->post_id) ->where( 'created_at', '>', DB::raw( "SELECT MAX(created_at) FROM comments WHERE post_id = ? AND author_id = ?", [$comment->post_id, $comment->author_id] ) ); }) ->where('user_id', '!=', $comment->post->author_id) ->get(); foreach ($users as $user) { $user->notify(new PostWasCommented($comment)); } 

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

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