title: How can I correctly implement UUIDs in Laravel with spatie/laravel-permission and webpatser/laravel-uuid?
body: <p>I have a Laravel 5.7 application using spatie/laravel-permission and normal IDs for models. I want to transition to using UUIDs and I've taken the steps mentioned in webpatser/laravel-uuid Readme file. Everything works with my own models, e.g. with User, Model A, Model B etc. and the relations seem fine, but I cannot seem to make the uuid's work with Spatie's permissions.</p>
  
  <p>When I want to assign a role (and associated permissions) to an object, I get the following error. This happens when I try to register a User and assign him a Role.</p>
  
  <p><a href="https://i.stack.imgur.com/WNX4X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WNX4X.png" alt="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"></a></p>
  
  <p>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</p>
  
  <p>I have defined a Uuids.php trait in my /app folder accordingly and added for all the models, including Permission.php and Role.php the following code:</p>
  
  <pre><code>public $incrementing = false;
  protected $keyType = 'string';
  
  use Uuids;
  
  protected $casts = [
      'id' =&gt; 'string',
  ];
  </code></pre>
  
  <p>I know that this works with any other model and relation, but just not with Spatie's permissions and it seems that the role_id is converted differently in the internal functions (like assignRole('') from a 36chars string to something else. If I query the Roles or Permissions I get the correct string id. </p>
  
  <p>Anything that I might be missing or does anyone knows a fix for this?</p>
  
  <p>Later edit: this is my original migration for Spatie:</p>
  
  <pre><code>    &lt;?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-&gt;uuid('id');
              $table-&gt;primary('id');
              $table-&gt;string('name');
              $table-&gt;string('guard_name');
              $table-&gt;timestamps();
          });
  
          Schema::create($tableNames['roles'], function (Blueprint $table) {
              $table-&gt;uuid('id');
              $table-&gt;primary('id');
              $table-&gt;string('name');
              $table-&gt;string('guard_name');
              $table-&gt;timestamps();
          });
  
          Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
              $table-&gt;uuid('permission_id');
              $table-&gt;string('model_type');
              $table-&gt;uuid($columnNames['model_morph_key']);
              $table-&gt;index([$columnNames['model_morph_key'], 'model_type', ]);
  
              $table-&gt;foreign('permission_id')
                  -&gt;references('id')
                  -&gt;on($tableNames['permissions'])
                  -&gt;onDelete('cascade');
  
              $table-&gt;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-&gt;uuid('role_id');
  
              $table-&gt;string('model_type');
              $table-&gt;uuid($columnNames['model_morph_key']);
              $table-&gt;index([$columnNames['model_morph_key'], 'model_type', ]);
  
              $table-&gt;foreign('role_id')
                  -&gt;references('id')
                  -&gt;on($tableNames['roles'])
                  -&gt;onDelete('cascade');
  
              $table-&gt;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-&gt;uuid('permission_id');
              $table-&gt;uuid('role_id');
  
              $table-&gt;foreign('permission_id')
                  -&gt;references('id')
                  -&gt;on($tableNames['permissions'])
                  -&gt;onDelete('cascade');
  
              $table-&gt;foreign('role_id')
                  -&gt;references('id')
                  -&gt;on($tableNames['roles'])
                  -&gt;onDelete('cascade');
  
              $table-&gt;primary(['permission_id', 'role_id']);
  
              app('cache')-&gt;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']);
      }
  }
  </code></pre>
  
  <p>This is an example of how the Roles and permissions are stored (working)</p>
  
  <p><a href="https://i.stack.imgur.com/k5Csa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k5Csa.png" alt="enter image description here"></a></p>
  
  <p>Same is for Permissions. So their _id is correct. The issue is somewhere in Laravel or in Spatie that it sends another value to the DB when trying to add a Role to a Model.</p>