简体   繁体   English

如何停止向自己发送通知?

[英]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. 您的第二个参数($ id)是指帖子ID。

You are saying that if your post_id does not equal your user_id, then send a notification. 您是说如果post_id与user_id不相等,请发送通知。 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. 我不确定如何设置逻辑,但是我想您还有另一个称为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. 更好的方法是在Like模型中创建一个指向Post模型的关系。 See here: https://laravel.com/docs/5.4/eloquent-relationships#one-to-many-inverse 看到这里: 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 希望这可以帮助

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

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