简体   繁体   中英

How can I stop sending Notification to myself?

When i like my own post i will get notification it should not happen,

now what i want is notification should not save in database if like I on my own post,

My controller :

    public function store(Request $request,$id)
    {
    $like = new Like();
    $like->user_id =Auth::user()->id;
    $like->post_id = $id;
    if($like->save())
    {
        if (Auth::user()->id != $id)
        {
            $user = User::findOrFail($request->get('user_id'));
            Notification::send($user , new likePost($like));
            $data = auth()->user()->name.'Liked Your Post '.'<Strong>'.$request->input('title').'</strong'.'<\br>'.'On'.Carbon::now();
            StreamLabFacades::pushMessage('test' , 'likePost' , $data);
        }
    }

Your second argument ($id) is referring to the post ID.

You are saying that if your post_id does not equal your user_id, then send a notification. In this case, there will only ever be one case of this.

I am not sure how your logic is set up, however I imagine you have another Model called Post.

Your logic would go something along the lines of:

public function store(Request $request, $post_id){

    $like = Like::create([ 'user_id' => Auth::user()->id, 'post_id' => $post_id]);

    $post = Post::whereId($post_id)->first();

    if( $post->user_id !== Auth::user()->id ) {
        //SEND Notification code here
    }

}

A better way of doing this would be to create a relantionship in your Like model that points to your Post model. See here: https://laravel.com/docs/5.4/eloquent-relationships#one-to-many-inverse

In particular the One-to-Many Inverse. They have an example with comments which I think is very similar to your case.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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