I have created the ability to add and remove friends in a laravel app using a pivot table and many to many relationship.
I am now stuck on the best way to handle blocking a member from friending another.
Right now I have a 'blocked' table with timestamps, blocked_user and created_by.
What would be the best way to check is that relationship exists in the block table before adding the friend?
Presuming you're using models. And that your User
model has a relationship blockedUsers
: Edit:
User extends Model {
// ...
public function isBlocked($userId)
{
return (boolean) $this->blockedUsers()
->where('blocked_user_id', $userId)->count();
}
public function blockedUsers()
{
return $this->belongsToMany(User::class, 'blocked_users_table', 'user_id', 'blocked_id');
}
}
Just call this method wherever you think fits. You could call it from addFriend
to check if this user is blocked or not.
the aproach could be like this:
$blocked_user = DB::table('blocked')
->where([
['created_by', '=', $user_id],
['blocked_user', '=', $blocked_user_id],
])
->first();
# user isn't blocked
if (empty($blocked_user)) {
# add friend procedure
}