Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems giving permissions to a role when the id field is a uuid type #2673

Closed
Aldamar opened this issue May 27, 2024 · 4 comments
Closed

Comments

@Aldamar
Copy link

Aldamar commented May 27, 2024

Description

After updating the migration file according to this guide , I configure my seeder to assign the default roles and permissions, so when I try to assign the permission to the role with the function $role->givePermissionTo($permission); it shows this error:
image

First I think it was a problem of UUID casting, so extended the model and indicate that the field must be cast to a string, but the problem reamins the same.

Steps To Reproduce

  1. Set the DB driver to mariadb.
  2. Update the migrations file according to this guide .
    1. Modify the migration on the primary id to get the default value from the Maria DB UUID function $table->uuid('id')->primary()->unique()->default(DB::raw('UUID()')) for the permissions and roles tables.
  3. Execute the migrations (with the seeder in the example)
  4. Try to assign a permission to a role

Example Application

https://github.com/Aldamar/laravel-permission-problems

Version of spatie/laravel-permission package:

6.7.0

Version of laravel/framework package:

v11.8.0

PHP version:

8.2

Database engine and version:

MariaDB LTS

OS: Windows/Mac/Linux version:

Debian in WSL2

@erikn69
Copy link
Contributor

erikn69 commented May 27, 2024

It seems to me that you are not adding your custom models to the configuration.

'permission' => Spatie\Permission\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,

@Aldamar
Copy link
Author

Aldamar commented May 27, 2024

Hi @erikn69
Even changing de models in the permission file the error remains
image

@erikn69
Copy link
Contributor

erikn69 commented May 27, 2024

On your repo https://github.com/Aldamar/laravel-permission-problems/tree/main/app/Models, i didn't found any UUID model
UUID models are tested

protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = \Str::uuid()->toString();
}
});
}
public function getIncrementing()
{
return false;
}
public function getKeyType()
{
return 'string';
}

protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = \Str::uuid()->toString();
}
});
}
public function getIncrementing()
{
return false;
}
public function getKeyType()
{
return 'string';
}
}

@Aldamar
Copy link
Author

Aldamar commented May 27, 2024

Ty @erikn69
Now I see my error 😅, sorry for taking your time 🙇🏻‍♂️.

@Aldamar Aldamar closed this as completed May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants