我已经实现了拉拉-许可来使用UUID主键自定义我的模型。然后我创建了一个播种机:
$viewemployee = Permission::create(['id' => Str::uuid(), 'name' => 'view employee']);
$A = Role::create(['id' => Str::uuid(), 'name' => 'A']);
但是,当我试图将权限分配给该角色时,使用:
$A->givePermissionTo($viewemployee);
当我运行种子时,它会显示出这样的错误
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivot>Table.php:139上的ErrorException非法偏移类型
然后,我尝试了另一种方法来分配:
$viewemployee->assignRole($A);
但是,它显示了另一个错误:
SQLSTATE23000\QueryException:完整性约束违反: 1452不能添加或更新子行:外键约束失败(
db
.role_has_permissions
,CONSTRAINTrole_has_permissions_role_id_foreign
REFERENCES (role_id
)在删除级联上引用roles
(id
) ) (SQL: insert insert到role_has_permissions
(permission_id
,role_id
)值(8f98272b-cbc3-459b-ab23-fc8fa86dd199,0))
我已经正确地跟踪了有关UUID和扩展的文档,但是我仍然无法找出我错过了什么,所以我无法运行我的种子。
role
和permission
数据已成功插入,在尝试插入到role_has_permissions
枢轴表时似乎遇到了麻烦。
下面是我对permissions
、roles
和role_has_permissions
表的迁移:
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');
});
发布于 2020-12-08 05:56:05
您需要将Str::uuid()
转换为字符串(string)Str::uuid()
。
$viewemployee = Permission::create(['id' => (string)Str::uuid(), 'name' => 'view employee']);
否则,ID的Permission
将是UUID对象,而不是UUID字符串。
https://stackoverflow.com/questions/63084241
复制相似问题