dongren9966 2017-05-22 21:43
浏览 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 22: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...

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

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法