On my Laravel app, I'm currently working on users being able to "block" other users.
In my user models, 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?
via Felix Maxime