dongren9966 2017-05-22 13:43 采纳率: 0%
浏览 60
已采纳

无法扭转Laravel中雄辩的关系

On my Laravel app, I'm currently working on users being able to "block" other users.

In my users model, I try to define the "blocked" relationships between users:

public function blockedOfMine() {
    return $this->belongsToMany('CommendMe\Models\User', 'blocked', 'user_id', 'blocked_id');
}

public function blockedBy() {
    return $this->belongsToMany('CommendMe\Models\User', 'blocked', 'blocked_id', 'user_id');
}

public function blocked() {
    return $this->blockedOfMine()->get();
}

public function blockedMe() {
    return $this->blockedBy()->get();
}

public function isBlocked(User $user) {
    return (bool) $this->blocked()->where('id', $user->id)->count();
}

public function hasBlockedMe(User $user) {
    return (bool) $this->blockedMe()->where('id', $user->id)->count();
}   

Basically, this model is only trying to figure out whether a user has blocked another user (function isBlocked) or whether a user has been blocked by another user (function hasBlockedMe).

Now, the first function works just fine, however the second one doesn't, despite them being completely the same:

if (Auth::user()->isBlocked($user)) {
   return redirect()->back()->with('info', 'You cannot message blocked users.');    
}
if (Auth::user()->hasBlockedMe($user)) {
   return redirect()->back()->with('info', 'This user has blocked communications with you.');   
}   

so if I try to message a user who is blocked, I get the message "You cannot message blocked users.", but the message goes through perfectly fine when I try to message a user who has blocked me.

What am I doing wrong?

展开全部

  • 写回答

1条回答 默认 最新

  • dongtang6718 2017-05-22 14:37
    关注

    Alright well I'm not sure if I should post this as an "answer" or not, seeing as the problem me being an idiot, but it turns out I was logged into the wrong account that uses the same avatar, so I got confused. This is why the message was being sent despite being "blocked". Whoops...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部