Laravel-permission: Add a description for a Role?

Created on 14 Sep 2017  路  3Comments  路  Source: spatie/laravel-permission

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'

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 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 {
    //
}

All 3 comments

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 {
    //
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

holymp2006 picture holymp2006  路  4Comments

antweny picture antweny  路  4Comments

enghelewa picture enghelewa  路  4Comments

bbdangar picture bbdangar  路  4Comments

ghost picture ghost  路  3Comments