Laravel的Eloquent方法getKey() returnsUUID的第一个数字

Laravel's Eloquent method getKey() returns the first number of the UUID

我有一个 User 和一个 Role 模型扩展 Catalyst's Sentinel 包。

如果我使用默认递增主键,一切正常。当我将其更改为使用 UUID 时,它的行为很奇怪。除了一些细节外,一切正常。

这是检查 User 是否属于 Rolecode snippet:

public function inRole($role)
{
    if ($role instanceof RoleInterface) {
        $roleId = $role->getRoleId();
    }
    foreach ($this->roles as $instance) {
        if ($role instanceof RoleInterface) {
            if ($instance->getRoleId() === $roleId) {
                return true;
            }
        } else {
            if ($instance->getRoleId() == $role || $instance->getRoleSlug() == $role) {
                return true;
            }
        }
    }
    return false;
}

该代码的作用是:如果我传递 Role 的实例,它会遍历所有用户的角色并检查它们是否与提供的 $role 的 ID 匹配。

这是 $user->roles ($this->roles) 关系:

public function roles()
{
    return $this->belongsToMany(static::$rolesModel, 'role_users', 'user_id', 'role_id')->withTimestamps();
}

$role->getRoleId() 正确 returns UUID: "8fbc99e2-3cbb-4e98-b0e3-4c88027d787f"

但是 $instance->getRoleId() returns 8 或者 UUID 的第一个数字字符是什么。如果第一个字符是字母那么它 returns 0.

我已经确认 $instance 确实是 RoleInterface 的一个实例,所以我认为它的行为应该与 $role.

完全相同

我还为两个模型添加了 public $incrementing = false

我已经对调用的所有供应商函数进行了几个检查点,但我不明白为什么会这样。

非常感谢任何帮助。

我在他们的 GitHub 存储库上开了一个新问题所以这里是 link 以保持答案同步:github.com/cartalyst/sentinel/issues/289(抱歉,我可以't post 超过 2 links)

这是包中的错误,已修复:https://github.com/cartalyst/sentinel/commit/239fa9ec88ce8fb955f5c0d85311d55a2f76e314

这个错误已经修复,但是如果你有很旧的 laravel 版本,它帮助我将这段代码添加到模型中

public function getKeyName() {
    if (isset($this->id)) {
        return 'id_'; // any non-existed column  
    } else {
        return 'id'; // real id column name 
    }
}

public function getKey() {
    return $this->id;
}