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...

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

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真