Tuesday 31 December 2019

Laravel - save many related models in saved event not work on update

I have three models to manage roles in my application: Role, User, UserRole.

In User model class there is an HasMany relation and in the database there is a foreign key in UserRole related table on user_code field:

public function userRoles(): HasMany
{
    return $this->hasMany('App\Models\UserRole', 'user_code', 'code');
}

I have a method for assign roles after saving an User model:

public function assignRole(...$roles)
{
    $userRoles = collect($roles)->flatten()->map(function($role) {
        return new UserRole(['code' => $role instanceof Role ? $role->code: $role]);
    });

    $this::saved(
        function ($object) use ($userRoles) {
            $object->userRoles()->delete();
            $object->userRoles()->saveMany($userRoles);
            $object->load('userRoles');
        }
    );

    return $this;
}

In UserController there is an update action for change roles to an User:

public function update(UpdateUser $request, User $user)
{
    $data = $request->all(); 
    $user->fill($data);
    $user->assignRole($data['roles']);

    if (!$user->save()) {
    ...

The problem is that when I update the $user I get an exception:

Illuminate\Database\QueryException

SQLSTATE[23503]: Foreign key violation...

This is caused by $object->userRoles()->saveMany($userRoles); in saved event.

I think it's due to the fact when it saves the related models the updated $user is not persisted to the database and the foreign key constraints on user_code are violated. But the saved event callback ** works well on create** user model. Why? And how can I resolve it?



from Laravel - save many related models in saved event not work on update

No comments:

Post a Comment