Laravel在spatie权限中的foreach循环中给出错误

时间:2019-01-23 04:10:10

标签: php laravel laravel-5 laravel-4 spatie

我正在应用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!'); 
}

Error Image

所以如果有人请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:2)

在您的提交表单中使用

<input type="checkbox" name="permissions[]" value="{{ $r->id }}"> {{$r->display_name}} </li>

在控制器中捕获这样的权限

$permissions = $request->permissions;