Laravel Entrust,如何将现有权限附加到角色

时间:2014-11-29 23:47:43

标签: php laravel

这是我正在使用的代码,它运行正常但是当我在数据库内部检查一切是否正常时我得到了这个结果

  

为什么我的权限ID和角色ID为0?

$developer = new Role();
$developer->name = 'Test';

$view_customers = new Permission();
$view_customers->name = 'view_customers';
$view_customers->display_name = 'View Customers';

$developer->attachPermission($view_customers->id);

2 个答案:

答案 0 :(得分:2)

试试这个

$developer = Role::where('name','=','Test')->get->first();

//if you want to attach new permission
$view_customers = new Permission();
$view_customers->name = 'view_customers';
$view_customers->display_name = 'View Customers';

//if you want to attach existing permission ex. you already have 'view_customers' in permission table 
$view_customers = Permission::where('name','=','view_customers')->get->first();

$developer->attachPermission($view_customers);

答案 1 :(得分:0)

attachPermission()适用于模型或数组,如果你想要atach或分离id,请使用

$developer->perms()->attach($view_customers->id);