简体   繁体   中英

UUID Spatie/Laravel-Permission SyncPermission Not Working

I use uuid as id in my posgtre sql with laravel framewok, i already change config permission.php

'model_morph_key' => 'model_uuid',

This is my Role model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Spatie\Permission\Models\Role as SpatieRole;

use App\Traits\Uuid;

class Role extends SpatieRole
{
  use Uuid;
  protected $primaryKey = 'id';
  public $incrementing  = false;
  protected $keyType    = 'string';

  /**
   * The attributes that should be cast to native types.
   *
   * @var array
   */
  protected $casts = [
      'id' => 'string'
  ];
}

This is my Permission model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Spatie\Permission\Models\Permission as SpatiePermission;

use App\Traits\Uuid;

class Permission extends SpatiePermission
{
  use Uuid;
  protected $primaryKey = 'id';
  public $incrementing  = false;
  protected $keyType    = 'uuid';

  /**
   * The attributes that should be cast to native types.
   *
   * @var array
   */
  protected $casts = [
      'id' => 'string'
  ];
}

In my controller

  public function setRolePermission(Request $request, $role)
  {
      //select role based role name
      $role = Role::findByName($role);

      $role->syncPermissions($request->permission);
      return redirect()->back()->with(['success' => 'Permission to Role Saved!']);
  }

When i use method sycnPermission return error like this

inner join "role_has_permissions" on "roles"."id" = "role_has_permissions"."role_id" where "role_has_permissions"."permission_id" in (1, 5, 8, 9, 14, 6354))

This (1, 5, 8, 9, 14, 6354)) is actually uuid datatype. But it is always knowing as integer datatype.

To resolve this problem, I'm did 2 things...

1. Create a Permission and Role models in project and extends from Spatie package, Add $keyType and $primaryKey attributes and trait Uuid .

App\\Model\\Role.php

<?php
namespace App\Model;

use App\Traits\Uuid;
use Spatie\Permission\Models\Role as SpatieRoles;

class Role extends SpatieRoles
{
    use Uuid;

    protected $keyType = 'string';
    protected $primaryKey = 'id';
}

App\\Model\\Permission .php

<?php
namespace App\Model;

use App\Traits\Uuid;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
    use Uuid;

    protected $keyType = 'string';
    protected $primaryKey = 'id';
}

2. Change the permission configuration in config/permission.php to reference in your new models created.

'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' => \App\Model\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' => \App\Model\Role::class,

    ],

Now, the Models will understanding a UUID with string and no but how integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM