Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hsl's avatar
Level 2

updateOrCreate does not create

Hi,

I'm trying to use updateOrCreate, I'm using the "id" as the field to check and if it needs to update it works fine, if it needs to create it tries to create a new row with the id 0.

Getting a bit lost here, anyone has an idea why this doesn't work?

0 likes
16 replies
jlrdw's avatar

Use fields other than id, other field that make the record unique: From docs

$flight = App\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99]
);
hsl's avatar
Level 2

Yeah that's not going to work, I need to do it with the id. But in the docs it doesn't state that it must be other fields than the id field, why wouldn't that work?

Simon_G's avatar

When you add the search id to the updated fields, this should be resolved:

User::UpdateOrCreate(
    ['id' => 4], 
    [
        'id' => 4,
        'name' => 'name', 
        'email' => '[email protected]', 
        'password' => \Hash::make('test'),
]);
hsl's avatar
Level 2

Unfortunately not, somehow the model that's returned by your example will have the id 0 and not the id 4 :(

1 like
staudenmeir's avatar

Please add your updateOrCreate() call and the model class.

jlrdw's avatar

Why do you want to change the id. The second array should only be what to change.

hsl's avatar
Level 2

Model:

class TestModel extends Model
{   
    // protected $primaryKey = 'id';

    protected $fillable = [
        'id','owner_id','name','legacy','active','default','count','enabled'
    ];

    public function owner()
    {
        return $this->belongsTo('App\Owner', 'owner_id');
    }

}

call:

$newData = array(
    'id' => $item['id'],
    'owner_id' => $owner->id,
    'name' => $item['name'],
    'legacy' => $item['legacy'],
    'count' => 0,
    'enabled' => true,
);

Log::info( $newData );

$testModel = TestModel::updateOrCreate( ["id" => $item['id']], $newData );

Log::error( $testModel );

The log info will show the right ID, in this case this: 'id' => 14406778943, With the log error it will show "id":0

I've also tried to do the call without the id in the object and that gives the exact same result.

jlrdw's avatar

It's probably updating everything except the ID, check. Usually an ID doesn't get updated.

hsl's avatar
Level 2

The id doesn't need to be updated, it needs to be created with the right id, so it doesn't really make sense that it doesn't do that.

Snapey's avatar

Why would you even have an ID if this is a create scenario?

hsl's avatar
Level 2

Why not? The item comes from a third party API and I need to store that with it's id.

Snapey's avatar
Snapey
Best Answer
Level 122

So you are not using auto incrementing id column?

Have you set $incrementing=false; in your model?

hsl's avatar
Level 2

Thanks, that was it,..

jlrdw's avatar

Why not? The item comes from a third party API and I need to store that with it's id.

And the reason you didn't share that at first ...

hsl's avatar
Level 2

@JLRDW - I don’t use the id field for auto incrementing columns anywhere in my apps so I didn’t think the origin of my data would be relevant.

PortNumber53's avatar

I'm beyond late to this party, but for me it was just the 'id' not being in the $fillable property

1 like

Please or to participate in this conversation.