Laravel 5.6 Eloquent 方法不返回数据

真正的爸爸

在我的User班级中,我有以下关系:

/**
 * Roles of a User
 *
 * @return \Illuminate\Database\Eloquent\Relations\hasManyThrough
 */
public function roles()
{
    return $this->hasManyThrough(Role::class, UserRole::class, 'user_id', 'id');
}

我创建了一个方法来根据传入的角色返回布尔值:

/**
 * Is User of a given Role?
 *
 * @return bool
 */
public function hasRole($roleShort = null)
{
    if (!$roleShort) {

        return false;

    }

    $result = $this->roles
        ->where('roles.short', '=', $roleShort)
        ->first();

    return $result ? true : false;
}

Tinker我得到第一个用户并返回他的角色,它按预期工作正常。但是当我将角色短名称传递给hasRole方法时,它总是返回 false。

>>> $u = User::find(1);
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
=> App\Models\User {#839
     id: 1,
     uuid: "4e86a284-ae1f-4753-a243-797dc5ce98fc",
     name: "Test User",
     email: "[email protected]",
     country_id: 11,
     partner_id: null,
     region_id: 1,
     language_id: 1,
     time_zone_id: 387,
     date_format_id: 1,
     time_format_id: 11,
     date_time_format_id: 13,
     activated_at: "2018-04-01 14:00:00",
     deleted_at: null,
     created_at: "2018-04-01 22:53:32",
     updated_at: "2018-04-01 22:53:32",
   }

>>> $u->roles
=> Illuminate\Database\Eloquent\Collection {#820
     all: [
       App\Models\Role {#827
         id: 1,
         partner: 0,
         name: "Admininstrator",
         short: "ADMIN",
         user_id: 1,
       },
     ],
   }

>>> $u->hasRole('ADMIN');
=> false

我错过了什么?我尝试记录 SQL 查询,但出现以下错误:

Log::info($this->roles
    ->where('roles.short', '=', $roleShort)
    ->first()
    ->toSql());

>>> $u->hasRole('ADMIN');
PHP Error:  Call to a member function toSql() on null in /home/vagrant/src/sdme/app/Models/User.php on line 126
富巴

您正在查询不正确的属性。你的属性short不是roles.short

此外,您可以使用该exists()方法来获得boolean结果。

/**
 * Is User of a given Role?
 *
 * @return bool
 */
public function hasRole($roleShort = null)
{
    if (! $roleShort) {
        return false;
    }

    return $this->roles()->where('short', $roleShort)->exists();
}

您也分别致电where()toSql()Collection,而不是一个Query\Builder实例。请注意roles我的答案后面的括号- roles()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用多个外键查询数据透视表Laravel 5 Eloquent

如何在Laravel 5中使用Eloquent更新数据透视表

与Eloquent Laravel 5的交易

Laravel Eloquent属于不工作

Laravel 5:如何使用Eloquent在数据透视表上进行联接查询?

Laravel Eloquent HasManyThrough返回空数组

Laravel 5:Eloquent的查询集合的“ orWhere”方法的替代方法

如何使用Laravel Eloquent select返回数组?

使用Eloquent / Laravel 5.x通用创建模型/数据库条目

Laravel 5命令不返回数据

我如何使用Eloquent&Laravel 5查询多态数据透视表

您可以使用Laravel 5在数据库中创建条目而不填充Eloquent模型吗

Laravel 5调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: tags();

如何使用Laravel Eloquent返回多个关系?

在 Laravel 中返回单个 Eloquent 对象

Laravel Eloquent Collection Search 总是返回 false

Laravel 5 - 覆盖从 Eloquent 获取或选择

Laravel 5/Vue JS:获取请求路由返回对象或数组的路由取决于 Eloquent 查询

Laravel 5.6 Eloquent 关系不返回值

Foreach 插入 Laravel 5 Eloquent

Laravel Eloquent - 数据透视表

Laravel Eloquent "with" 返回与 limit 的关系

Laravel 5 Eloquent where with multiple or in Clauses

Laravel 5 Eloquent AND 而不是 where

Laravel eloquent 耗时 6-12 秒返回结果

Laravel 5.8 Eloquent Create() 返回错误的 Id

Laravel update eloquent 不更新记录

Laravel Eloquent 关系返回 null

laravel 9.0 Eloquent all() 方法不返回关系