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



Laravel 5.6.3 委托:1.9

我正在尝试使用此播种机仅播种一行

<?php
use IlluminateDatabaseSeeder;
use AppmodelsPermission;
use AppmodelsRole;
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 Appmodels;
use IlluminateDatabaseEloquentModel;
use ZizacoEntrustEntrustRole;
use AppmodelsPermission;
use AppUser;
class Role extends EntrustRole
{
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
public function users()
{
return $this->belongsToMany(User::class);
}
}

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

D:workwwwmyapp>php artisan db:seed --class=PermissionSeeder
D:workwwwmyapp>php artisan db:seed --class=RoleSeeder
SymfonyComponentDebugExceptionFatalThrowableError  : Class 'AppPermission' not found
at D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseEloquentConcernsHasRelationships.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   IlluminateDatabaseEloquentModel::newRelatedInstance("AppPermission")
D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseEloquentConcernsHasRelationships.php:418
2   IlluminateDatabaseEloquentModel::belongsToMany("AppPermission", "permission_role", "role_id", "permission_id")
D:workwwwmyappvendorzizacoentrustsrcEntrustTraitsEntrustRoleTrait.php:80
Please use the argument -v to see more details.

Using -v
D:workwwwmyapp>php artisan db:seed --class=RoleSeeder -v
SymfonyComponentDebugExceptionFatalThrowableError  : Class 'AppPermission' not found
at D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseEloquentConcernsHasRelationships.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   IlluminateDatabaseEloquentModel::newRelatedInstance("AppPermission")
D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseEloquentConcernsHasRelationships.php:418
2   IlluminateDatabaseEloquentModel::belongsToMany("AppPermission", "permission_role", "role_id", "permission_id")
D:workwwwmyappvendorzizacoentrustsrcEntrustTraitsEntrustRoleTrait.php:80
3   ZizacoEntrustEntrustRole::perms()
D:workwwwmyappvendorzizacoentrustsrcEntrustTraitsEntrustRoleTrait.php:177
4   ZizacoEntrustEntrustRole::attachPermission()
D:workwwwmyappdatabaseseedsRoleSeeder.php:23
5   RoleSeeder::run()
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:29
6   call_user_func_array([])
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:29
7   IlluminateContainerBoundMethod::IlluminateContainer{closure}()
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:87
8   IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationApplication), Object(Closure))
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:31
9   IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), [])
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerContainer.php:564
10  IlluminateContainerContainer::call()
D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseSeeder.php:122
11  IlluminateDatabaseSeeder::__invoke()
D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseConsoleSeedsSeedCommand.php:63
12  IlluminateDatabaseConsoleSeedsSeedCommand::IlluminateDatabaseConsoleSeeds{closure}()
D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseEloquentConcernsGuardsAttributes.php:122
13  IlluminateDatabaseEloquentModel::unguarded(Object(Closure))
D:workwwwmyappvendorlaravelframeworksrcIlluminateDatabaseConsoleSeedsSeedCommand.php:64
14  IlluminateDatabaseConsoleSeedsSeedCommand::handle()
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:29
15  call_user_func_array([])
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:29
16  IlluminateContainerBoundMethod::IlluminateContainer{closure}()
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:87
17  IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationApplication), Object(Closure))
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerBoundMethod.php:31
18  IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), [])
D:workwwwmyappvendorlaravelframeworksrcIlluminateContainerContainer.php:564
19  IlluminateContainerContainer::call()
D:workwwwmyappvendorlaravelframeworksrcIlluminateConsoleCommand.php:179
20  IlluminateConsoleCommand::execute(Object(SymfonyComponentConsoleInputArgvInput), Object(IlluminateConsoleOutputStyle))
D:workwwwmyappvendorsymfonyconsoleCommandCommand.php:251
21  SymfonyComponentConsoleCommandCommand::run(Object(SymfonyComponentConsoleInputArgvInput), Object(IlluminateConsoleOutputStyle))
D:workwwwmyappvendorlaravelframeworksrcIlluminateConsoleCommand.php:166
22  IlluminateConsoleCommand::run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
D:workwwwmyappvendorsymfonyconsoleApplication.php:886
23  SymfonyComponentConsoleApplication::doRunCommand(Object(IlluminateDatabaseConsoleSeedsSeedCommand), Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
D:workwwwmyappvendorsymfonyconsoleApplication.php:262
24  SymfonyComponentConsoleApplication::doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
D:workwwwmyappvendorsymfonyconsoleApplication.php:145
25  SymfonyComponentConsoleApplication::run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
D:workwwwmyappvendorlaravelframeworksrcIlluminateConsoleApplication.php:89
26  IlluminateConsoleApplication::run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
D:workwwwmyappvendorlaravelframeworksrcIlluminateFoundationConsoleKernel.php:122
27  IlluminateFoundationConsoleKernel::handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
D:workwwwmyappartisan:37
D:workwwwmyapp>

更新

我在修补匠上试过

>>> $permission = AppmodelsPermission::first();
=> AppmodelsPermission {#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 = AppmodelsRole::first();
=> AppmodelsRole {#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 存储库的底部,它说

如果你的应用使用自定义命名空间,则需要告诉委托 在您的权限和榜样所在的地方,您可以通过编辑来做到这一点config/entrust.php中的配置文件

因此,在您的情况下,您必须将权限模型命名空间更改为默认值AppmodelsPermission

默认设置为AppPermission

因此config/entrust.php编辑文件并更改permission

'permission' => 'AppmodelsPermission'

'role' => 'AppmodelsRole'

相关内容

  • 没有找到相关文章

最新更新