'Unable to update user role spite package when datatype changed to UUID
Hi I am using Spaite package for roles and permissions. I am using UUID datatype. Everything works fine when used normal integer datatype.
Now showing Following Error when update User Role
"
There is no role named
e0cce77e-908c-4ddd-b097-bdd6b5aefceb"
View page
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-6 col-12">
<label for="role" class="form-label">Role</label>
{{$roleData[0]->id}}
<select class="form-control"
name="role" required>
<option value="">Select role</option>
@foreach($roleData as $role)
<option value="{{ $role->id }}"
{{ in_array($role->name, $userRole)
? 'selected'
: '' }}>{{ $role->name }} </option>
@endforeach
</select>
@if ($errors->has('role'))
<span class="text-danger text-left">{{ $errors->first('role') }}</span>
@endif
</div>
user controller
public function update(User $user, UpdateUserRequest $request)
{
User::where('id', $user->id)
->update([
'name' => $request->name,
'phone' => $request->phone,
'street' => $request->street,
'city' => $request->city,
'state' => $request->state,
'description' => $request->description,
'zip' => $request->zip
]);
DB::enableQueryLog();
$user->syncRoles($request->get('role'));
$quries = DB::getQueryLog();
print_r($quries); exit;
return redirect()->route('users.index')
->withSuccess(__('User updated successfully.'));
}
User model
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use jeremykenedy\LaravelRoles\Traits\HasRoleAndPermission;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoleAndPermission;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Role model extended spaite role
<?php
namespace App\Models;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
use Spatie\Permission\Models\Role as SpatieRole;
class Role extends SpatieRole
{
use HasFactory, HasRoles;
use Uuid;
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
public function getIncrementing()
{
return false;
}
public function getKeyType()
{
return 'string';
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
