Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What changes are needed if I'm using UUIDs with Spatie


What changes are needed if I'm using UUIDs with Spatie


To use UUIDs with Spatie's Laravel Permission package, several key changes are needed in your migrations, models, and configuration to properly support UUIDs instead of the default auto-incrementing integer IDs. Here are the detailed steps and considerations:

1. Update the Migrations

- After publishing the package migrations (`php artisan vendor:publish`), you need to modify the permission tables migration to use UUID columns instead of unsigned big integers for the primary keys and foreign keys.
- Replace the `id` columns in the `roles` and `permissions` tables with UUID columns, e.g., `$table->uuid('id')->primary();`.
- Update pivot tables (`model_has_roles`, `model_has_permissions`, `role_has_permissions`) to use UUID columns for foreign keys such as `role_id` and `permission_id`.
- Also, ensure the morph key column in `model_has_roles` and `model_has_permissions` (usually `model_id`) is changed to UUID if your user or other models use UUIDs.
- Add foreign key constraints referencing UUID `id` columns.
- Run the updated migrations to apply these changes[1][3][6].

2. Extend and Modify the Role and Permission Models

- Create your own `Role` and `Permission` models extending Spatie’s models.
- In these models:
- Use the `HasUuids` trait from Laravel or a custom UUID trait to automatically generate UUIDs.
- Set the `$primaryKey` property to `'id'` (default) but ensure the key type is string by setting `protected $keyType = 'string';`.
- Set `public $incrementing = false;` to disable auto-incrementing.
- Optionally, cast the `id` to string.
- Example:

php
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{
    use HasUuids;

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

- Do the same for `Permission` model[2][3][6].

3. Update Configuration

- In `config/permission.php`, update the `models` array to point to your custom `Role` and `Permission` models instead of the default ones.
- This ensures the package uses your UUID-enabled models.

php
'models' => [
    'role' => App\Models\Role::class,
    'permission' => App\Models\Permission::class,
],

4. Adjust User Model and Morph Keys

- If your `User` model uses UUIDs, ensure that the morph key column in the `model_has_roles` and `model_has_permissions` tables matches the UUID type.
- Update the `$primaryKey`, `$keyType`, and `$incrementing` properties in your `User` model accordingly.
- Also, ensure that the morph map or morph keys used by the package correspond to UUID strings.

5. Handle Assigning Roles and Permissions

- When assigning roles or permissions, ensure you use the UUID string values.
- Some internal package functions may expect integer IDs by default, so overriding models and properly casting keys to strings is essential to avoid errors where UUIDs are treated as integers.
- Clear the permission cache after making these changes using `php artisan permission:cache-reset` or `app('cache')->forget('spatie.permission.cache');`.

Summary of Key Points

- Change migrations to use UUID columns for all relevant IDs and foreign keys.
- Extend Spatie’s Role and Permission models to use UUIDs (`HasUuids` trait, disable incrementing, set key type string).
- Update `config/permission.php` to use your custom models.
- Ensure morph keys and user model keys are UUIDs.
- Clear cache after changes.

Following these steps allows seamless integration of UUIDs with Spatie’s Laravel Permission package, avoiding issues with type mismatches and ensuring UUIDs are correctly handled throughout the package[1][2][3][6].

Citations:
[1] https://spatie.be/docs/laravel-permission/v6/advanced-usage/uuid
[2] https://github.com/spatie/laravel-permission/blob/main/docs/advanced-usage/uuid.md
[3] https://stackoverflow.com/questions/53411027/how-can-i-correctly-implement-uuids-in-laravel-with-spatie-laravel-permission-an
[4] https://spatie.be/docs/laravel-permission/v6/installation-laravel
[5] https://github.com/spatie/laravel-permission/issues/1584
[6] https://stackoverflow.com/a/53411550
[7] https://laracasts.com/discuss/channels/code-review/spatie-package-with-uuid
[8] https://stackoverflow.com/questions/53408670/how-can-i-use-uuid-in-spatie-laravel-permission