php - Getting an error when associating role to a user for one to many relationship
Get the solution ↓↓↓I have a one to many relationship between and
{-code-2}
. I am unable to associate a role to a user while saving or updating a user, as its throwing me an error while saving the model. Please spare some time to have a look below.
SQLSTATE[22007]: Inval{-code-10} datetime format: 1366 Incorrect integer value: '[3]' for column
{-code-3}
..
{-code-5}
at row 1 (SQL: updateset
{-code-5}
= [3],.
{-code-9}
= 2021-01-29 04:44:19 where{-code-10}
= 3)
- Database Structure
Users
Answer
Answer
- User Model
{-code-12}
- Role Model
public function users() {
return $this->hasMany(User::class);
}
- User Controller
public $showUserUpdationModal = false;
public $role;
public User $user;
protected $rules = [
'user.name' => 'required|string|max:255',
'user.email' => 'required|string|email|max:255',
'role' => 'required',
];
public function storeUser()
{
$this->validate();
$this->validate([
'user.email' => 'unique:users,email,'.$this->user->id,
]);
$this->user->role()->associate($this->role);
$this->user->save();
$this->showUserUpdationModal = false;
$this->dispatchBrowserEvent('notify', $this->user->name.' Updated Successfully');
$this->emit('sectionRefresh');
}
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.