public function users_by_state()
 {
     $states = State::with('users')->get();
     $data = [];
     foreach ($states as $state) {
         $data[] = ['label' => $state->name, 'value' => $state->users->count()];
     }
     return response()->json($data);
 }
 public function delete($id)
 {
     $state = State::findOrFail($this->decode($id));
     if ($state->users->count() > 0) {
         alert(trans('messages.state.not_deleted'), 'danger');
         return redirect()->back();
     }
     $state->delete();
     alert(trans('messages.state.deleted'));
     return redirect()->back();
 }
 public function profile_update(ProfileRequest $request)
 {
     // Comprobar si el email existe solo si se ha cambiado el email
     if ($request->email != auth()->user()->email) {
         $user = User::whereEmail($request->email)->get();
         if (!empty($user)) {
             alert('El email ingresado ya existe', 'danger');
             return redirect()->back();
         }
     }
     $state = State::findOrFail($request->state);
     $user = auth()->user();
     $user->state()->associate($state);
     $user->update($request->all());
     alert('Se modificaron los datos con éxito');
     return redirect()->back();
 }
 public function update($hash_id, UserRequest $request)
 {
     $id = $this->decode($hash_id);
     $form = collect_clean($request->all());
     $usuario = User::findOrFail($id);
     $usuario->update($form->toArray());
     // actualizando departamento
     if ($request->has('state')) {
         if ($request->state != $usuario->state_id) {
             $state = State::findOrFail($request->state);
             $usuario->state()->associate($state);
             $usuario->save();
         }
     }
     alert('Se modificó el usuario correctamente');
     return redirect()->route('admin::usuarios::create');
 }
 public function showRegister()
 {
     $states = State::lists('name', 'id')->toArray();
     return view('register')->with('states', $states);
 }