Я пытаюсь получить пользователей, которые не имеют роли владельца, но также пропускает пользователей, которые не имеют никакой роли. Пользователи могут иметь одну или несколько ролей. Я хочу, чтобы все пользователи имели несколько ролей или вообще не имели роли, но если пользователь содержит роль владельца, то игнорировать должен только этот пользователь.
Примечание: я использую spatie/laravel-permission
, который получает роли пользователей из модели, промежуточные таблицы ролей
Вот мой запрос объема
public function scopeForCompany(EloquentBuilder $query, string $customerId): EloquentBuilder
{
$query->where(function (EloquentBuilder $q) {
$q->doesntHave('roles');
$q->orHas('roles');
});
$query->whereHas('roles', function (EloquentBuilder $q) {
$q->whereNotIn('name', ['owner']);
});
return $query->where('customer_id', $customerId);;
}
вот тест
public function it_apply_query_scope_to_get_customer_specific_users_only(): void
{
$model = new User;
// create non customer users
\factory(User::class, 2)->create();
$customer = \factory(Customer::class)->create();
foreach (['owner', 'admin', 'user'] as $role) {
$role = \factory(Role::class)->create(['name' => $role]);
$user = \factory(User::class)->create(['customer_id' => $customer->id]);
$user->roles()->save($role);
}
$scopedUsers = $model->newQuery()->forCompany($customer->id)->get();
$nonScopedUsers = $model->newQuery()->get();
static::assertCount(2, $scopedUsers); // Failed asserting that actual size 0 matches expected size 2.
static::assertCount(5, $nonScopedUsers);
}
Отладка: вот запрос строки:
"select * from `users` where (not exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_uuid` and `model_has_roles`.`model_type` = ?) or exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_uuid` and `model_has_roles`.`model_type` = ?)) and exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_uuid` and `model_has_roles`.`model_type` = ? and `name` not in (?)) and `customer_id` = ? and `users`.`deleted_at` is null"
Это то, что я попробовал первым, но не сработало
return $query->whereHas('roles', function (EloquentBuilder $query): void {
$query->whereNotIn('name', ['owner']);
})->where('customer_id', $customerId);
Любая помощь будет оценена спасибо