我正在应用Spatie Laravel权限并进行编码以向角色添加权限。 并遇到错误。
public function store(Request $request) {
//Validate name and permissions field
$this->validate($request, [
'name'=>'required|unique:'.config('permission.table_names.roles').'|max:10',
]
);
$name = $request['name'];
$role = new Role();
$role->name = $name;
$permissions = $request['permissions'];
$role->save();
//Looping thru selected permissions
if(count($permissions>0)){
foreach ($permissions as $permission) {
$p = Permission::where('id', '=', $permission)->firstOrFail();
//Fetch the newly created role and assign permission
$role = Role::where('name', '=', $name)->first();
$role->givePermissionTo($p);
}
}
return redirect()->route('roles.index')
->with('flash_message',
'Role'. $role->name.' added!');
}
所以如果有人请帮助我解决这个问题。
答案 0 :(得分:2)
在您的提交表单中使用
<input type="checkbox" name="permissions[]" value="{{ $r->id }}"> {{$r->display_name}} </li>
在控制器中捕获这样的权限
$permissions = $request->permissions;