UUID/ULID | laravel-permission | Spatie       

 SPATIE  

  Laravel Permission 
=====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-permission](https://spatie.be/docs/laravel-permission/v8)  Advanced-usage  UUID/ULID

 Version   v8   v7   v6   v5   v4   v3      

 Other versions for crawler [v8](https://spatie.be/docs/laravel-permission/v8) [v7](https://spatie.be/docs/laravel-permission/v7) [v6](https://spatie.be/docs/laravel-permission/v6) [v5](https://spatie.be/docs/laravel-permission/v5) [v4](https://spatie.be/docs/laravel-permission/v4) [v3](https://spatie.be/docs/laravel-permission/v3) 

  UUID/ULID    
- [ Introduction ](https://spatie.be/docs/laravel-permission/v8/introduction)
- [ Support us ](https://spatie.be/docs/laravel-permission/v8/support-us)
- [ Prerequisites ](https://spatie.be/docs/laravel-permission/v8/prerequisites)
- [ Installation in Laravel ](https://spatie.be/docs/laravel-permission/v8/installation-laravel)
- [ Upgrading ](https://spatie.be/docs/laravel-permission/v8/upgrading)
- [ Questions and issues ](https://spatie.be/docs/laravel-permission/v8/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-permission/v8/changelog)
- [ About us ](https://spatie.be/docs/laravel-permission/v8/about-us)

Basic Usage
-----------

- [ Basic Usage ](https://spatie.be/docs/laravel-permission/v8/basic-usage/basic-usage)
- [ Direct Permissions ](https://spatie.be/docs/laravel-permission/v8/basic-usage/direct-permissions)
- [ Using Permissions via Roles ](https://spatie.be/docs/laravel-permission/v8/basic-usage/role-permissions)
- [ Enums ](https://spatie.be/docs/laravel-permission/v8/basic-usage/enums)
- [ Teams permissions ](https://spatie.be/docs/laravel-permission/v8/basic-usage/teams-permissions)
- [ Wildcard permissions ](https://spatie.be/docs/laravel-permission/v8/basic-usage/wildcard-permissions)
- [ Blade directives ](https://spatie.be/docs/laravel-permission/v8/basic-usage/blade-directives)
- [ Defining a Super-Admin ](https://spatie.be/docs/laravel-permission/v8/basic-usage/super-admin)
- [ Using multiple guards ](https://spatie.be/docs/laravel-permission/v8/basic-usage/multiple-guards)
- [ Artisan Commands ](https://spatie.be/docs/laravel-permission/v8/basic-usage/artisan)
- [ Middleware ](https://spatie.be/docs/laravel-permission/v8/basic-usage/middleware)
- [ Passport Client Credentials Grant usage ](https://spatie.be/docs/laravel-permission/v8/basic-usage/passport)
- [ Example App ](https://spatie.be/docs/laravel-permission/v8/basic-usage/new-app)

Best Practices
--------------

- [ Roles vs Permissions ](https://spatie.be/docs/laravel-permission/v8/best-practices/roles-vs-permissions)
- [ Model Policies ](https://spatie.be/docs/laravel-permission/v8/best-practices/using-policies)
- [ Performance Tips ](https://spatie.be/docs/laravel-permission/v8/best-practices/performance)

Advanced usage
--------------

- [ Testing ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/testing)
- [ Database Seeding ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/seeding)
- [ Exceptions ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/exceptions)
- [ Extending ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/extending)
- [ Cache ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/cache)
- [ Events ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/events)
- [ Custom Permission Check ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/custom-permission-check)
- [ UUID/ULID ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/uuid)
- [ PhpStorm Interaction ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/phpstorm)
- [ Other ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/other)
- [ Timestamps ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/timestamps)
- [ UI Options ](https://spatie.be/docs/laravel-permission/v8/advanced-usage/ui-options)

 UUID/ULID
=========

###  On this page 

1. [ Migrations ](#content-migrations)
2. [ Configuration (OPTIONAL) ](#content-configuration-optional)
3. [ Models ](#content-models)

If you're using UUIDs (ULID, GUID, etc) for your User models or Role/Permission models there are a few considerations to note.

> NOTE: THIS IS NOT A FULL LESSON ON HOW TO IMPLEMENT UUIDs IN YOUR APP.

Since each UUID implementation approach is different, some of these may or may not benefit you. As always, your implementation may vary.

We use "uuid" in the examples below. Adapt for ULID or GUID as needed.

Migrations
--------------------------------------------------------------------------------------

You will need to update the `create_permission_tables.php` migration after creating it with `php artisan vendor:publish`. After making your edits, be sure to run the migration!

**User Models using UUIDs**If your User models are using `uuid` instead of `unsignedBigInteger` then you'll need to reflect the change in the migration provided by this package. Something like the following would be typical, for **both** `model_has_permissions` and `model_has_roles` tables:

```
// note: this is done in two places in the default migration file, so edit both places:
-   $table->unsignedBigInteger($columnNames['model_morph_key'])
+   $table->uuid($columnNames['model_morph_key'])
```

**Roles and Permissions using UUIDS**If you also want the roles and permissions to use a UUID for their `id` value, then you'll need to change the id fields accordingly, and manually set the primary key.

```
    Schema::create($tableNames['permissions'], function (Blueprint $table) {
-         $table->bigIncrements('id'); // permission id
+         $table->uuid('uuid')->primary()->unique(); // permission id
//...
    });

    Schema::create($tableNames['roles'], function (Blueprint $table) {
-         $table->bigIncrements('id'); // role id
+         $table->uuid('uuid')->primary()->unique(); // role id
//...
    });

    Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
-         $table->unsignedBigInteger($pivotPermission);
+         $table->uuid($pivotPermission);
        $table->string('model_type');
//...
        $table->foreign($pivotPermission)
-             ->references('id') // permission id
+             ->references('uuid') // permission id
            ->on($tableNames['permissions'])
            ->onDelete('cascade');
//...

    Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
-         $table->unsignedBigInteger($pivotRole);
+         $table->uuid($pivotRole);
//...
        $table->foreign($pivotRole)
-             ->references('id') // role id
+             ->references('uuid') // role id
            ->on($tableNames['roles'])
            ->onDelete('cascade');//...

    Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
-         $table->unsignedBigInteger($pivotPermission);
-         $table->unsignedBigInteger($pivotRole);
+         $table->uuid($pivotPermission);
+         $table->uuid($pivotRole);

         $table->foreign($pivotPermission)
-             ->references('id') // permission id
+             ->references('uuid') // permission id
            ->on($tableNames['permissions'])
            ->onDelete('cascade');

         $table->foreign($pivotRole)
-             ->references('id') // role id
+             ->references('uuid') // role id
            ->on($tableNames['roles'])
            ->onDelete('cascade'); 
```

Configuration (OPTIONAL)
----------------------------------------------------------------------------------------------------------------------------

You might want to change the pivot table field name from `model_id` to `model_uuid`, just for semantic purposes. For this, in the `permission.php` configuration file edit `column_names.model_morph_key`:

- OPTIONAL: Change to `model_uuid` instead of the default `model_id`.

```
        'column_names' => [    
        /*
         * Change this if you want to name the related pivots other than defaults
         */
        'role_pivot_key' => null, //default 'role_id',
        'permission_pivot_key' => null, //default 'permission_id',

        /*
         * Change this if you want to name the related model primary key other than
         * `model_id`.
         *
         * For example, this would be nice if your primary keys are all UUIDs. In
         * that case, name this `model_uuid`.
         */
-             'model_morph_key' => 'model_id',
+             'model_morph_key' => 'model_uuid',
        ],
```

- If you extend the models into your app, be sure to list those models in your `permissions.php` configuration file. See the Extending section of the documentation and the Models section below.

Models
--------------------------------------------------------------------------

If you want all the role/permission objects to have a UUID instead of an integer, you will need to Extend the default Role and Permission models into your own namespace in order to set some specific properties. (See the Extending section of the docs, where it explains requirements of Extending, as well as the `permissions.php` configuration settings you need to update.)

Examples:

Create new models, which extend the Role and Permission models of this package, and add Laravel's `HasUuids` trait (available since Laravel 9):

```
php artisan make:model Role
php artisan make:model Permission
```

`App\Model\Role.php`

```
<?php
namespace App\Models;

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

class Role extends SpatieRole
{
    use HasFactory;
    use HasUuids;
    protected $primaryKey = 'uuid';
}
```

`App\Model\Permission.php`

```
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
    use HasFactory;
    use HasUuids;
    protected $primaryKey = 'uuid';
}
```

And edit `config/permission.php`

```
    'models' => [

        /*
         * When using the "HasPermissions" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your permissions. Of course, it
         * is often just the "Permission" model but you may use whatever you like.
         *
         * The model you want to use as a Permission model needs to implement the
         * `Spatie\Permission\Contracts\Permission` contract.
         */

-         'permission' => Spatie\Permission\Models\Permission::class
+         'permission' => \App\Models\Permission::class,

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your roles. Of course, it
         * is often just the "Role" model but you may use whatever you like.
         *
         * The model you want to use as a Role model needs to implement the
         * `Spatie\Permission\Contracts\Role` contract.
         */

-         'role' => Spatie\Permission\Models\Role::class,
+         'role' => \App\Models\Role::class,

    ],
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
