使用 UUID 将权限分配给 spatie/laravel-permission 中的角色时出现非法偏移类型
Illegal offset type when assigning Permission into Role in spatie/laravel-permission using UUID
我已经实现 laravel-permission 使用 UUID 主键自定义我的模型。然后我创建了一个播种机:
$viewemployee = Permission::create(['id' => Str::uuid(), 'name' => 'view employee']);
$A = Role::create(['id' => Str::uuid(), 'name' => 'A']);
但是,当我尝试使用以下方式将权限分配给角色时:
$A->givePermissionTo($viewemployee);
当我运行种子
时,它显示这样的错误
ErrorException
Illegal offset type
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivot>Table.php:139
然后,我尝试了另一种方法来赋值:
$viewemployee->assignRole($A);
但是,它显示另一个错误:
Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (db
.role_has_permissions
, CONSTRAINT role_has_permissions_role_id_foreign
FOREIGN KEY (role_id
) REFERENCES roles
(id
) ON DELETE CASCADE) (SQL: insert into role_has_permissions
(permission_id
, role_id
) values (8f98272b-cbc3-459b-ab23-fc8fa86dd199, 0))
我已经正确地遵循了关于 UUID 和扩展的文档,但我仍然无法弄清楚我错过了什么,所以我无法 运行 我的种子。
role
和 permission
数据已成功插入,尝试插入到 role_has_permissions
pivot table.
时似乎遇到了麻烦
这是我对 permissions
、roles
和 role_has_permissions
tables 的迁移:
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->uuid('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
$table->primary('id');
});
Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->uuid('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
$table->primary('id');
});
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->uuid('permission_id');
$table->uuid('role_id');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
});
您需要将 Str::uuid()
转换为字符串 (string)Str::uuid()
$viewemployee = Permission::create(['id' => (string)Str::uuid(), 'name' => 'view employee']);
否则 ID 的 Permission
将是 UUID 对象而不是 UUID 字符串。
我已经实现 laravel-permission 使用 UUID 主键自定义我的模型。然后我创建了一个播种机:
$viewemployee = Permission::create(['id' => Str::uuid(), 'name' => 'view employee']);
$A = Role::create(['id' => Str::uuid(), 'name' => 'A']);
但是,当我尝试使用以下方式将权限分配给角色时:
$A->givePermissionTo($viewemployee);
当我运行种子
时,它显示这样的错误ErrorException Illegal offset type at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivot>Table.php:139
然后,我尝试了另一种方法来赋值:
$viewemployee->assignRole($A);
但是,它显示另一个错误:
Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
db
.role_has_permissions
, CONSTRAINTrole_has_permissions_role_id_foreign
FOREIGN KEY (role_id
) REFERENCESroles
(id
) ON DELETE CASCADE) (SQL: insert intorole_has_permissions
(permission_id
,role_id
) values (8f98272b-cbc3-459b-ab23-fc8fa86dd199, 0))
我已经正确地遵循了关于 UUID 和扩展的文档,但我仍然无法弄清楚我错过了什么,所以我无法 运行 我的种子。
role
和 permission
数据已成功插入,尝试插入到 role_has_permissions
pivot table.
这是我对 permissions
、roles
和 role_has_permissions
tables 的迁移:
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->uuid('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
$table->primary('id');
});
Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->uuid('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
$table->primary('id');
});
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->uuid('permission_id');
$table->uuid('role_id');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
});
您需要将 Str::uuid()
转换为字符串 (string)Str::uuid()
$viewemployee = Permission::create(['id' => (string)Str::uuid(), 'name' => 'view employee']);
否则 ID 的 Permission
将是 UUID 对象而不是 UUID 字符串。