Skip to content
Advertisement

Laravel: UpdateOrCreate on Relation?

A user hasOne car.

users

JavaScript

cars

JavaScript

Inside the User class I have

JavaScript

I want to call updateOrCreate on the relation Model like this:

JavaScript

However, I get the error message

“SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘id’ in ‘where clause’ (SQL: update cars set color = red, energy = 0.1, updated_at = 2018-01-12 15:26:47 where id is null)

Why is he looking for

id is null

?

Advertisement

Answer

This is how I solved my problem without adding an unnecessary auto incrementing id to the cars model:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement