public function update($id, $userData) { if (isset($userData['password']) && !empty(trim($userData['password']))) { $userData['password'] = bcrypt($userData['password']); } else { unset($userData['password']); } $user = $this->user->find($id); $user->update($userData); if (\Entrust::hasRole('admin')) { $role = $this->role->where('name', $userData['role'])->first(); $user->roles()->detach(); $user->attachRole($role); } }
/** * Attach role as admin for default provided user */ protected function attachRoleForAdmin() { $this->line(''); $this->info(sprintf('Attach role for default admin')); $adminEmail = $this->ask("Enter default admin email"); $user = User::where('email', '=', $adminEmail)->first(); $role = Role::where('name', '=', 'admin')->first(); if ($user) { if ($user->hasRole('admin')) { $this->error(sprintf('%s has been already been assigned with admin role', $adminEmail)); } else { $user->attachRole($role); $this->info(sprintf('%s has been assigned with admin role', $adminEmail)); } } else { $adminName = $this->ask("Enter default admin name"); $adminPassword = $this->ask("password"); $user = User::create(['name' => $adminName, 'email' => $adminEmail, 'phone_number' => '', 'address' => '', 'password' => bcrypt($adminPassword), 'is_active' => '1']); $user->attachRole($role); $this->info(sprintf('Admin User created with email : %s', $adminEmail)); } }