Is it possible to add a description or a title for a role?
We're getting the roles by name but I would like to add a readable name for example
'admin' => 'Administrator'
You should be able to create your own Role model with any custom properties you like. Make sure to implement the Spatie\Permission\Contracts\Role contract and update models.role in the config file.
Let me know if you need any help!
Is there an example Model of this? do we need to copy all the code from the default model in order for this to work? and probably also change the migration to add a new field?
Thank you!
Yep, you'll need to change the migration to include anything you might need.
I just realised: if you only need to add some basic properties to the Role model (for example a readable_name) you can just update the migration and create your Roles like you would with any other model:
$role = Role::create([
'name' => 'admin',
'readable_name' => 'Administrator',
'another_custom_property' => 'foo',
]);
If you really need a custom model (maybe you want to add some custom methods or mutators) you can extend the Spatie\Permission\Models\Role model which will end up looking something like this:
<?php
namespace App\Role;
use Spatie\Permission\Models\Role as BaseRole;
class Role extends BaseRole {
//
}
Most helpful comment
Yep, you'll need to change the migration to include anything you might need.
I just realised: if you only need to add some basic properties to the
Rolemodel (for example areadable_name) you can just update the migration and create yourRoles like you would with any other model:If you really need a custom model (maybe you want to add some custom methods or mutators) you can extend the
Spatie\Permission\Models\Rolemodel which will end up looking something like this: