Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

Assigning permission to role while seeding - Entrust #930

Closed PrafullaKumarSahu closed 6 years ago

PrafullaKumarSahu commented 6 years ago

I tried doing this

<?php

use Illuminate\Database\Seeder;
use App\models\Permission;
use App\models\Role;

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
       $role =  Role::create([
            'name' => 'super-admin',
            'display_name' => 'Super Admin',
            'description' => 'This will be one permission, that can not be assigned or created.'
        ]);

        $permission = App\models\Permission::first()->where('name', 'super-admin')->first();
        $role->attachPermission($permission);
    }
}

I just tried it in tinker

>>> $permission = App\models\Permission::first();
=> App\models\Permission {#2923
     id: 1,
     name: "super-admin",
     display_name: "Super Admin",
     description: "This will be one permission, that can not be assigned or created.",
     created_at: "2018-08-31 05:01:08",
     updated_at: "2018-08-31 05:01:08",
   }
>>> $role = App\models\Role::first();
=> App\models\Role {#2925
     id: 1,
     name: "super-admin",
     display_name: "Super Admin",
     description: "This will be one permission, that can not be assigned or modified.",
     created_at: "2018-08-31 05:01:10",
     updated_at: "2018-08-31 05:01:10",
   }
>>> $role->attachPermission($permission);
PHP Error:  Class 'App/Permission' not found in D:/work/www/myapp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php on line 656
>>>

But getting error, as it is trying to search Permission model on App/ and not in App/models.

You can check more information on this . I asked it in stackoverflow, but it seems you are the correct person to help me here.

PrafullaKumarSahu commented 6 years ago

The issue was I need to change the namespace of Permission and Role model in config/entrust.php file.

'permission' => 'App\models\Permission',
'role' => 'App\models\Role',