spatie / laravel-permission

Associate users with roles and permissions
https://spatie.be/docs/laravel-permission
MIT License
12.17k stars 1.78k forks source link

Custom permission contract for model not working #2726

Closed Jeordy closed 1 month ago

Jeordy commented 1 month ago

Description

I want to switch to UUIDs and have followed the steps for implementing them. I also created the Role and Permission models accordingly. But, when using the HasUuids trait, I get an error:

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (Connection: mysql, SQL: insert intopermissions(guard_name,name,description,updated_at,created_at) values (web, users.view, {"en":"View Users","nl":"Toon Gebruikers"}, 2024-09-26 12:35:57, 2024-09-26 12:35:57)) So, the trait is not triggered.

Then I added a custom boot function to the model, like so:

protected static function boot() { parent::boot(); static::creating(function ($model) { if (empty($model->{$model->getKeyName()})) { $model->{$model->getKeyName()} = Str::uuid()->toString(); } }); }

But it's not going through this model, even a dump in the boot function does not appear...

In permission config I added:

`'models' => [ /*

The correct custom models are configured.

Does anyone have the same issue?

Steps To Reproduce

Follow the documentation on changing to UUID's.

Example Application

No response

Version of spatie/laravel-permission package:

6.9.0

Version of laravel/framework package:

11.16.0

PHP version:

8.3.11

Database engine and version:

Mysql 8.0.36

OS: Windows/Mac/Linux version:

Mac/Windows

Jeordy commented 1 month ago

Sorry, it seems that the model is skipped when using seeders.

drbyte commented 1 month ago

Sorry, it seems that the model is skipped when using seeders.

If you specify ('use') the intended model in your seeder, it will use it.

Jeordy commented 1 month ago

Sorry, it seems that the model is skipped when using seeders.

If you specify ('use') the intended model in your seeder, it will use it.

Thank you, the seeder was using the spatie model directly, not our own.