6

A user hasOne car.

users

id | name
1  | Bob
2  | Alice

cars

idMember |  color  |  energy
   1     |  blue   |    0.95

Inside the User class I have

public function car()
{
  return $this->hasOne('App\Car','idMember');
}

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

$user->car()->updateOrCreate(['idMember' => $user->id], ['color' => 'red', 'energy' => '0.1']); 

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

?

3
  • Laravel expect each entity to have an auto-incrementing primary key with the name id , having a primary key is also a relational requirement (though it may be a composite key which laravel does not support). Just save your sanity and add an auto incrementing id column in that table so everything has a unique id. Also by convention laravel expects the foreign key to be <singular other table>_id i.e. in your case idMember should be user_id (but you can change that in your hasOne parameters) Commented Jan 12, 2018 at 15:41
  • @apokryfos oh yeah your right.. forgot about that. But I rather change the primaryKeyof my car model since no AI key is needed in a hasOne relation. Commented Jan 12, 2018 at 15:48
  • On one to one relationships having the foreign key as the primary key as you have done is also an option. Commented Jan 12, 2018 at 15:51

4 Answers 4

14

To be clear, your original syntax is correct. You can use it on a relation:

$user->car()->updateOrCreate(['idMember' => $user->id], [
    'color' => 'red',
    'energy' => '0.1',
]);

This will check if there is a car with idMember === $user->id; if so, update color and energy. If not, it will create a car record with idMember, color, and energy.

I haven't tested, but based on the first parameter's array type, you should be able to pass in multiple match conditions, such as

['idMember' => $user->id, 'day' => 'tuesday']
Sign up to request clarification or add additional context in comments.

1 Comment

updateOrCreate / upserts in laravel doc and laravel news 1
2

Your cars model should have a primary key, commonly called "id". Create it.

1 Comment

Thank you! That of course one way how to solve it. But I prefere the solution witouth AI in cars table
1

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

class Car extends Model
{

  protected $primaryKey   = 'idMember';
  public    $incrementing = false;

2 Comments

That is fine now, but you'll run into trouble if you're at some point going to decide that a User can hasMany Cars, because at that point there is nothing left in the cars table that is 'quaranteed' to be unique.
@JeffreyWesterkamp well for a car a hasone makes not really sense, because people may ahve more then one.. but in my complicated real life scenario I have a hasone relation that will never be a hasmany :). But even if I would want to change it, all I would have to do would change hasOne to hasMany, add the AI and remove the two top lines from the Car model.
-3

In your model mention the primaryKey if primarykey is not "ID"

protected $primaryKey = "Your Primary Key";

If you do not need icnrementing for your primary key

public    $incrementing = false;

Thats it ..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.