I have a User
model and a Blocklist
model, Now when I call User::all()
I only want to return Users who are not related based on records in the block list table.
For instance:
user
Table
id | … |
---|---|
1 | … |
2 | … |
3 | … |
4 | … |
5 | … |
blocklist
Table
id | user_id | blockable_id | blockable_type |
---|---|---|---|
1 | 1 | 2 | AppModelsUser |
2 | 1 | 3 | AppModelsUser |
2 | 5 | 1 | AppModelsUser |
User
Model
...
public function blocklist()
{
return $this->hasMany(Block::class);
}
Blocklist
Model
...
public function blockable(): MorphTo
{
return $this->morphTo();
}
UserController
...
$users = AppModelsUser::whereDoesntHave('blocklist', function (Builder $query) {
$query->where('user_id', auth('sanctum')->id());
$query->orWhere(function (Builder $query) {
$query->where('blockable_id', auth('sanctum')->id());
$query->where('blockable_type', User::class);
});
})->all();
The idea is that, if user: 1
is making this request they should not see user: 2
and user: 3
whom they have blocked and they should also not see user: 5
who has also blocked them, but this is not how it works, whatever I do all the users are still returned.