php - Eloquent:根据关系过滤记录

标签 php mysql laravel eloquent

我正在尝试获取没有所有者角色的客户特定用户,但它也会跳过没有任何角色的用户。用户可以具有一个或多个角色。我想让所有用户要么拥有多个角色,要么根本没有角色,但如果用户包含所有者角色,则只应忽略该用户。

注意:我正在使用 spatie/laravel-permission 从模型中获取用户角色 has roles 中间表

这是我的范围查询

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);

任何帮助将不胜感激

最佳答案

您需要执行一些 Or 逻辑才能使其发生。我将查询分为 3 个部分。

声明:“我想让所有用户都拥有多个角色”

$query->has('roles', '>=', 2);

接下来你想要所有没有角色:“或者根本没有角色”。

$query->doesntHave('roles');

最后,您的查询正确地过滤掉了角色不能是所有者的地方。

$query->whereHas('roles', function (EloquentBuilder $query): void {
   $query->whereNotIn('name', ['owner']);
})

将它们放在一起做一些类似的事情,使用一个子 where 查询。正确执行您想要的 Or 逻辑。

$query->where(function($builder){
    $builder->has('roles', '>=', 2);
    $builder->whereHas('roles', function (EloquentBuilder $query): void {
       $query->whereNotIn('name', ['owner']);
    })
});

$builder->orDoesntHave('roles');

在伪逻辑语句中,这看起来类似于:

(roles.each.name != 'owner' && count(roles) >= 2) || empty(roles)

让我们看看这是否对您的情况有帮助,否则发布构建器的 toSql() 并让我们弄清楚。这是一个相当复杂的查询构建器逻辑。

关于php - Eloquent:根据关系过滤记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059196/

相关文章:

php - 循环遍历数据库字段 PHP MySQL

php - 摆脱 mysql 查询中重复的客户 ID

php - 将 Eloquent 模型与业务逻辑分离

Laravel Eloquent get() 按主键 id 索引

php - Laravel 5 - Controller 中的路由和可变参数

php - 如何从 php 和 mysql 向客户端发出递增 key ?

php - 在 PHP 中使用与包含

列类型更新后 MySQL 表 "crashed"

mysql - MySQL 中的 Hive 外部表

mysql - 构建具有多个内连接的 SQL 查询的最佳方法?