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

axeloz's avatar

updateOrCreate not touching timestamps?

Hello,

I have a question of my own. It seems that updateOrCreate doesn't touch created_at and updated_at when creating a new row. I'm very surprised, I don't recall such a behavior with Laravel. Plus the documentation says:

The upsert method will automatically set the created_at and updated_at timestamps

This concerns the upsert method but how comes upsert and updateOrCreate have different behaviors?

My snippet:

Payment::updateOrInsert(
		['charge_id' => $charge_id],
		[
			'user_id'		=> $user->id,
			'price_id'		=> $price->id,
			'status'		=> $status
		]
);

Thanks

0 likes
11 replies
Snapey's avatar

updateOrInsert is a QueryBuilder method ?

axeloz's avatar

@Snapey yes but as I was calling it from the ORM Payment::class model, I’d assume that I’d work fine

Sinnbeck's avatar

@axeloz if a method does not exist in the eloquent builder for models, the call just gets forwarded to the regular query builder. This for instance what happens with toSql(). Regular insert would also work in eloquent, but would skip timestamps

axeloz's avatar

The upsert method doc is three lines below the updateOrCreate method in the ORM section of the doc. So you'd expect same behavior.

Snapey's avatar
Snapey
Best Answer
Level 122

@axeloz but you are using updateOrInsert and then reading the docs for updateOrCreate

Sinnbeck's avatar

@axeloz it uses updateOrCreate or upsert in those examples? You use updateOrInsert?

Snapey's avatar

upserts are something else (you are not using upsert)

axeloz's avatar

@Sinnbeck @snapey I'm so sorry, I just realized when you posted your replies. You are right, I'm not using the correct method. It was in front of my eyes.

Now, who's reply should I upvote for best answer? :-)

Please or to participate in this conversation.