user11352561
user11352561

Reputation: 2647

Laravel - How to add extra column to Laravel Spatie Permission

In my Laravel-8 project, I am using Spatie-Permission. I want to add extra column, company_id to the Spatie Role table. In the migration, I did this:

php artisan make:migration add_company_column_to_role_tables

Also, in the config/permission:

    //'permission' => Spatie\Permission\Models\Permission::class,
    'permission' => App\Models\Permission::class,

    //'role' => Spatie\Permission\Models\Role::class,
    'role' => App\Models\Role::class,

I want each company to create its roles, but the permissions remain the same. What else do I need to do in order to make it work?

Thanks

Upvotes: 0

Views: 3074

Answers (1)

Dr4v
Dr4v

Reputation: 385

I recently face this. I needed to have an additional display_name for my permissions. People on the spatie github repo said it was better to use Laravel translations in the next fashion:

__('your string permission') 

But I found that not that suitable. So I did this:

  1. Added a migration: php artisan make:migration addCustomColumnsToPermissionsTable

  2. Then, added the following:

    public function up() {
        Schema::table('permissions', function (Blueprint $table){
            $table->string('display_name')->after('name');
        });
    }
    public function down() {
        Schema::table('permissions', function (Blueprint $table){
            $table->dropColumn('display_name');
        });
    }
    

3.- finally run: php artisan migrate

And voila! you have your new column. Hope that helps :D

Upvotes: 0

Related Questions