Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudMonem's avatar

How to prevent sending notification if user is replying on his own comments

I have 3 tables .. users | comments | replies

and I have a notification called RepliedToComment.php

 public function toDatabase($notifiable)
    {
        return [
            'repliedTime'=>Carbon::now(),
            'reply'=>$this->reply, 
            'user'=>$notifiable
        ];
    }

My RepliesController.php looks like that

  public function store(Request $request)
    {
        $reply = Reply::create($request->all());
    
        if ($reply && $reply->comment && $reply->comment->user ) {
            
            $reply->comment->user->notify(new RepliedToComment($reply));
            
            return back()->with('success', 'Reply Submitted Successfully');
        }
        return back()->with('error', 'Something wrong while submitting this reply!');
    }

Now the notification is working fine when someone reply on a comment, but I would like to "not" send the notification if the reply is made by the comment owner himself.

I tried to add .. if reply->comment->user == Auth::user() in the store function, but it didn't work.

0 likes
3 replies
Wakanda's avatar
Wakanda
Best Answer
Level 10

@mahmoudmonem

 public function store(Request $request)
    {
        $reply = Reply::create($request->all());
	$comment = // to the comment you are replying
	
	if ($comment->user_id === auth()->id) {
		return back()->with('success', 'Reply Submitted Successfully');
	}
    
        if ($reply && $reply->comment && $reply->comment->user ) {
            
            $reply->comment->user->notify(new RepliedToComment($reply));
            
            return back()->with('success', 'Reply Submitted Successfully');
        }
        return back()->with('error', 'Something wrong while submitting this reply!');
    }
1 like
MahmoudMonem's avatar

Hello @loyd

I did this and it worked as I want .. thank you so much :)

 public function store(Request $request)
    {
        $reply = Reply::create($request->all());

        $comment = $reply->comment;
	
	if ($comment->user_id === Auth::user()->id) {
		return back()->with('success', 'Reply Submitted Successfully');
	}
    
        if ($reply && $reply->comment && $reply->comment->user ) {
            
            $reply->comment->user->notify(new RepliedToComment($reply));
            
            return redirect()->back()->with('success', 'Reply Submitted Successfuly');
        }
        return redirect()->back()->with(['error', 'Something wrong while creating reply!']);
    }

Please or to participate in this conversation.