播种时为角色分配权限 - 委托

Assigning permission to role while seeding - Entrust

Laravel 5.6.3 委托:1.9

我试图用这个播种机只播种一行

<?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 = Permission::first()->where('name', 'super-admin')->first();
        $role->attachPermission($permission);
    }
}

和型号

<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use Zizaco\Entrust\EntrustRole;

use App\models\Permission;
use App\User;

class Role extends EntrustRole
{
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

现在,当我尝试播种时,出现此错误。

D:\work\www\myapp>php artisan db:seed --class=PermissionSeeder

D:\work\www\myapp>php artisan db:seed --class=RoleSeeder

   Symfony\Component\Debug\Exception\FatalThrowableError  : Class 'App\Permission' not found

  at D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php:656
    652|      * @return mixed
    653|      */
    654|     protected function newRelatedInstance($class)
    655|     {
  > 656|         return tap(new $class, function ($instance) {
    657|             if (! $instance->getConnectionName()) {
    658|                 $instance->setConnection($this->connection);
    659|             }
    660|         });

  Exception trace:

  1   Illuminate\Database\Eloquent\Model::newRelatedInstance("App\Permission")
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php:418

  2   Illuminate\Database\Eloquent\Model::belongsToMany("App\Permission", "permission_role", "role_id", "permission_id")
      D:\work\www\myapp\vendor\zizaco\entrust\src\Entrust\Traits\EntrustRoleTrait.php:80

  Please use the argument -v to see more details.


Using -v

D:\work\www\myapp>php artisan db:seed --class=RoleSeeder -v

   Symfony\Component\Debug\Exception\FatalThrowableError  : Class 'App\Permission' not found

  at D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php:656
    652|      * @return mixed
    653|      */
    654|     protected function newRelatedInstance($class)
    655|     {
  > 656|         return tap(new $class, function ($instance) {
    657|             if (! $instance->getConnectionName()) {
    658|                 $instance->setConnection($this->connection);
    659|             }
    660|         });

  Exception trace:

  1   Illuminate\Database\Eloquent\Model::newRelatedInstance("App\Permission")
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php:418

  2   Illuminate\Database\Eloquent\Model::belongsToMany("App\Permission", "permission_role", "role_id", "permission_id")
      D:\work\www\myapp\vendor\zizaco\entrust\src\Entrust\Traits\EntrustRoleTrait.php:80

  3   Zizaco\Entrust\EntrustRole::perms()
      D:\work\www\myapp\vendor\zizaco\entrust\src\Entrust\Traits\EntrustRoleTrait.php:177

  4   Zizaco\Entrust\EntrustRole::attachPermission()
      D:\work\www\myapp\database\seeds\RoleSeeder.php:23

  5   RoleSeeder::run()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29

  6   call_user_func_array([])
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29

  7   Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:87

  8   Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure))
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:31

  9   Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), [])
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\Container.php:564

  10  Illuminate\Container\Container::call()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Seeder.php:122

  11  Illuminate\Database\Seeder::__invoke()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:63

  12  Illuminate\Database\Console\Seeds\SeedCommand::Illuminate\Database\Console\Seeds\{closure}()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:122

  13  Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:64

  14  Illuminate\Database\Console\Seeds\SeedCommand::handle()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29

  15  call_user_func_array([])
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29

  16  Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:87

  17  Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure))
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:31

  18  Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), [])
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Container\Container.php:564

  19  Illuminate\Container\Container::call()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Console\Command.php:179

  20  Illuminate\Console\Command::execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      D:\work\www\myapp\vendor\symfony\console\Command\Command.php:251

  21  Symfony\Component\Console\Command\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Console\Command.php:166

  22  Illuminate\Console\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      D:\work\www\myapp\vendor\symfony\console\Application.php:886

  23  Symfony\Component\Console\Application::doRunCommand(Object(Illuminate\Database\Console\Seeds\SeedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      D:\work\www\myapp\vendor\symfony\console\Application.php:262

  24  Symfony\Component\Console\Application::doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      D:\work\www\myapp\vendor\symfony\console\Application.php:145

  25  Symfony\Component\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Console\Application.php:89

  26  Illuminate\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:122

  27  Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      D:\work\www\myapp\artisan:37

D:\work\www\myapp>

更新

我在 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
>>>

正确的做法是什么?

https://github.com/Zizaco/entrust 回购的底部它说

If your app uses a custom namespace then you'll need to tell entrust where your permission and role models are, you can do this by editing the config file in config/entrust.php

因此在您的情况下,您必须将权限模型命名空间更改为 App\models\Permission 默认设置为 App\Permission

所以编辑 config/entrust.php 文件并更改 permission

'permission' => 'App\models\Permission'

'role' => 'App\models\Role'