mardi 22 janvier 2019

Laravel giving error in foreach loop in spatie-permission

I was applying Spatie Laravel-permissions and coding to add permissions to roles. and faced a error.

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

So if someone please help me solve this problem.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire