为角色表分配权限php,php - 在播种时为角色分配权限 - 委托 - SO中文参考 - www.soinside.com...

Laravel 5.6.3委托:1.9

我试图用这台播种机播种一行

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);

}

}

和模型

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>

更新

我试着修补它

>>> $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

>>>

这样做的正确方法是什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值