'updated_at' in field list is ambiguous in Laravel

1

I have the following error in a laravel query, the query that I execute is the following one.

$usuarioE->roles()->update(['kaseya_rol_user.state'=>0]);

and the error thrown at 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 it could be solved by specifying the name of the table for updated_at but I do not know how to do it if that field is added by laravel

    
asked by FuriosoJack 22.06.2017 в 00:13
source

2 answers

0

Unfortunately there is no easy solution for now using Eloquent, apart from using the Query Builder to generate the same query.

This bug was solved in a moment for MySQL, but the fix did not work for PostgreSQL, so it was reversed correction.

You can see a possible solution using Eloquent in this SO question in English: link

Here is the link to the bug report on GitHub: link

    
answered by 22.06.2017 в 00:31
0

My solution starts from the relationship I have is from many to many

A user has several roles and one role has several users.

In the pivot table there is a field called state that serves to know the status of the user's role, so I have a history of the roles he had.

as what I need is to update precisely is that field of the role that the user no longer has.

Then I'm going to use the updateExistingPivot method, which updates the indicated fields of a pivot table, using it in the following way.

  $usuarioE->roles()->updateExistingPivot($rolTMP->id,['state'=>0]);

therefore I need to know first the id of the role to which I have to update the variable $ rolTMP- > id .

  

In the question we wanted to update all the user roles in a single stroke, with this solution it is necessary to know what the user's roles are then go through them and apply the previous query.

    
answered by 23.06.2017 в 16:51