Laravel 在 spatie-permission 的 foreach 循环中给出错误
Laravel giving error in foreach loop in spatie-permission
我正在应用 Spatie Laravel-permissions 和编码来为角色添加权限。
遇到错误。
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!');
}
所以如果有人请帮我解决这个问题。
在您的提交表单中使用此
<input type="checkbox" name="permissions[]" value="{{ $r->id }}"> {{$r->display_name}} </li>
在控制器中像这样捕获这些权限
$permissions = $request->permissions;
我正在应用 Spatie Laravel-permissions 和编码来为角色添加权限。 遇到错误。
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!');
}
所以如果有人请帮我解决这个问题。
在您的提交表单中使用此
<input type="checkbox" name="permissions[]" value="{{ $r->id }}"> {{$r->display_name}} </li>
在控制器中像这样捕获这些权限
$permissions = $request->permissions;