I have the following error in a laravel query, the query that I execute is the following.
$usuarioE->roles()->update(['kaseya_rol_user.state'=>0]);
and the error it throws me is
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'updated_at' i
n field list is ambiguous (SQL: update `kaseya_roles` inner join `kaseya_ro
l_user` on `kaseya_roles`.`id` = `kaseya_rol_user`.`rol_id` set `kaseya_rol
_user`.`state` = 0, `updated_at` = 2017-06-21 22:03:59 where `kaseya_rol_us
er`.`user_id` = 11)
I know that it could be solved by specifying the name of the table for updated_at but I don't know how to do it if that field is added by Laravel
Unfortunately there is no easy solution for now using Eloquent, other than using the Query Builder to generate the same query.
This bug was fixed at one point for MySQL, but the fix didn't work for PostgreSQL, so the fix was reverted.
You can see a possible solution using Eloquent in this English SO question: https://stackoverflow.com/a/25325707/495103
Here is the link to the bug report on GitHub: https://github.com/laravel/framework/issues/13909
My solution is based on the fact that the relationship I have is many to many
A user has multiple roles and a role has multiple users.
In the pivot table there is a field called state that is used to know the status of the user's role, so I keep a history of the roles he had.
as what I need is to update precisely that field of the role that the user no longer has.
So I'm going to use the updateExistingPivot method which precisely updates the fields that are indicated in a pivot table. I use it in the following way.
therefore I first have to know the id of the role to which I have to update, that is why the variable $rolTMP->id .