So i am using the latest codeigniter 4, I am trying to edit/ update user information. however its just running me in a loop for the first check.
What I am trying to do is check if the data has changed, if it has not validate and save the new information if there is a problem return with validation errors.
However when adding
if (!$user->hasChanged()) {
return redirect()->back()
->with("message", "Nothing to update.");
}
to the mix if i change the username it does not update it just gives message nothing to update
public function update($id)
{
$users = auth()->getProvider();
$user = $users->findById($id);
$validation = ConfigServices::validation();
$validation->setRules([
'username' => 'required|is_unique[users.username]',
'email' => 'required|is_unique[auth_identities.secret]'
]);
$data = [
'username' => $this->request->getPost('username'),
'email' => $this->request->getPost('email')
];
if (!$user->hasChanged()) {
return redirect()->back()
->with("message", "Nothing to update.");
}
if ($validation->run($data)) {
$user->fill([
'username' => $data['username'],
'email' => $data['email']
]);
$users->update($user);
return redirect()->to("admin/dashboard/manage-users")
->with("message", "User updated.");
}
return redirect()->back()
->with('errors', $validation->getErrors())
->withInput();
}