I'm trying to hide users with a certain scope, but that scope is not applied it seems, because the query still returns the users I've wanted to hide. Any idea what I'm doing wrong? The rest of the query works as expected.
// User model
public function scopeNotResigned(Builder $query)
{
return $query->where(fn($query) => $query->whereNull('date_of_resignation')->orWhere('date_of_resignation', '>', now()));
}
// PhoneNumber model
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
protected function getPhoneNumbers()
{
return PhoneNumber::query()
->when(
filled($this->search),
function (Builder $query): Builder {
return $query->where(function (Builder $query): Builder {
return $query->where('name', 'like', "%{$this->search}%")
->orWhere('phone', 'like', "%{$this->search}%")
->orWhereHas('user', function (Builder $query): Builder {
return $query->notResigned() // HERE
->where('abbreviation', 'like', "%{$this->search}%")
->orWhereHas('categories', function (Builder $query): Builder {
return $query->where('name', 'like', "%{$this->search}%");
});
});
});
},
)
->when(blank($this->search), fn (Builder $query): Builder => $query->where('id', '<', 0)) // Show 0 results by default.
->with(['user', 'user.categories'])
->paginate($this->tableRecordsPerPage);
}