如何使用spatie / laravel-permission和webpatser / laravel-uuid在Laravel中正确实现UUID?

时间:2018-11-21 11:21:23

标签: php laravel permissions uuid spatie

我有一个使用Spatie / laravel-permission和正常ID用于模型的Laravel 5.7应用程序。我想过渡到使用UUID,并且已经采取了webpatser / laravel-uuid自述文件中提到的步骤。一切都适用于我自己的模型,例如与用户,模型A,模型B等之间的关系似乎很好,但是我似乎无法在Spatie的许可下使uuid正常工作。

当我想给对象分配角色(和相关权限)时,出现以下错误。当我尝试注册用户并为其分配角色时,会发生这种情况。

As you can see in this image, the role_id from Spatie is transmitted into this query as an integer. Now it is 342, in other cases it is similar to 705293

如您在该图中所看到的,Spatie中的role_id作为整数传输到此查询中。现在是342,在其他情况下类似于705293

我已经在/ app文件夹中定义了一个Uuids.php特性,并为所有模型(包括Permission.php和Role.php)添加了以下代码:

public $incrementing = false;
protected $keyType = 'string';

use Uuids;

protected $casts = [
    'id' => 'string',
];

我知道这适用于任何其他模型和关系,但不适用于Spatie的权限,而且似乎在内部函数(例如assignRole(''))中,role_id从36chars字符串转换为其他字符串的方式有所不同。我查询“角色”或“权限”时,获得了正确的字符串ID。

有什么我可能会想念的东西吗?或者没人知道解决办法吗?

后来编辑:这是我最初对Spatie的迁移:

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePermissionTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $tableNames = config('permission.table_names');
        $columnNames = config('permission.column_names');

        Schema::create($tableNames['permissions'], function (Blueprint $table) {
            $table->uuid('id');
            $table->primary('id');
            $table->string('name');
            $table->string('guard_name');
            $table->timestamps();
        });

        Schema::create($tableNames['roles'], function (Blueprint $table) {
            $table->uuid('id');
            $table->primary('id');
            $table->string('name');
            $table->string('guard_name');
            $table->timestamps();
        });

        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
            $table->uuid('permission_id');
            $table->string('model_type');
            $table->uuid($columnNames['model_morph_key']);
            $table->index([$columnNames['model_morph_key'], 'model_type', ]);

            $table->foreign('permission_id')
                ->references('id')
                ->on($tableNames['permissions'])
                ->onDelete('cascade');

            $table->primary(
                ['permission_id', $columnNames['model_morph_key'], 'model_type'],
                    'model_has_permissions_permission_model_type_primary'
            );
        });

        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
            $table->uuid('role_id');

            $table->string('model_type');
            $table->uuid($columnNames['model_morph_key']);
            $table->index([$columnNames['model_morph_key'], 'model_type', ]);

            $table->foreign('role_id')
                ->references('id')
                ->on($tableNames['roles'])
                ->onDelete('cascade');

            $table->primary(
                ['role_id', $columnNames['model_morph_key'], 'model_type'],
                    'model_has_roles_role_model_type_primary'
            );
        });

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

            app('cache')->forget('spatie.permission.cache');
        });
    }

    //Reverse the migrations.
    public function down()
    {
        $tableNames = config('permission.table_names');

        Schema::drop($tableNames['role_has_permissions']);
        Schema::drop($tableNames['model_has_roles']);
        Schema::drop($tableNames['model_has_permissions']);
        Schema::drop($tableNames['roles']);
        Schema::drop($tableNames['permissions']);
    }
}

这是如何存储(工作)角色和权限的示例

enter image description here

权限相同。因此,他们的_id是正确的。问题出在Laravel或Spatie中的某个地方,当试图向模型中添加角色时,它将向数据库发送另一个值。

2 个答案:

答案 0 :(得分:0)

不建议更新软件包中的表。因此,在app文件夹中创建一个特征:

php artisan migrate:make change_primary_key_type_in_roles_table --table=roles

创建迁移以更新您的表结构:

public function up()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->uuid('id')->change();
    });
}

public function down()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->increments('id')->change();
    });
}

在迁移中添加以下内容:

<?php

namespace YourNamespace;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use Uuids;

    protected $guarded = [''];
    protected $casts = [
        'id' => 'string',
    ];

在模型中使用Uuid特性:

composer dumpautoload

然后php artisan migratestd::deque

答案 1 :(得分:0)

也面临这个问题。通过使用以下代码更改Permission.php和Role.php来解决:

public $incrementing = false;
protected $keyType = 'string';

use Uuids;

protected $casts = [
    'id' => 'uuid',
];