Problem with $user->syncGroups #277
-
I don't know exactly if I am doing something wrong or there is a problem with syncGroups() If I do this: $groups = "'" . implode("', '", $this->request->getPost('groups')) . "'";
$user->syncGroups($groups); Returns this error:
if I manually insert the groups: $user->syncGroups('admin','user'); then it works... |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
It is true, See https://github.com/codeigniter4/shield/blob/develop/docs/quickstart.md#adding-a-group-to-a-user |
Beta Was this translation helpful? Give feedback.
-
Thanks kenjis, but what I'm trying to do is not add a group to the user while creating. But I'm trying to change groups for a user while editing. In fact if I write the groups dynamically it gives me the error, but if I write the command manually in the code it works. This does not work: $data = [
'firstname' => $this->request->getPost('firstname'),
'lastname' => $this->request->getPost('lastname'),
'email' => $this->request->getPost('email'),
'company' => $this->request->getPost('company')
];
$user = $this->userModel->findById($id);
$user->fill($data);
if ($this->userModel->save($user))
{
$groups = "'" . implode("', '", $this->request->getPost('groups')) . "'";
$user->syncGroups($groups);
}
This works: $data = [
'firstname' => $this->request->getPost('firstname'),
'lastname' => $this->request->getPost('lastname'),
'email' => $this->request->getPost('email'),
'company' => $this->request->getPost('company')
];
$user = $this->userModel->findById($id);
$user->fill($data);
if ($this->userModel->save($user))
{
$user->syncGroups('admin', 'user');
} |
Beta Was this translation helpful? Give feedback.
-
Do you have any suggestions? Because I have tried several solutions I still have not been able to solve. |
Beta Was this translation helpful? Give feedback.
-
Thank you kenjis I solved it, I admit it was something completely new to me, I didn't know this possibility existed.
|
Beta Was this translation helpful? Give feedback.
Thank you kenjis I solved it, I admit it was something completely new to me, I didn't know this possibility existed.
$user->syncGroups(...$this->request->getPost('groups'));