Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

WasCreated, IsDirty and Other Checks If Model Was Changed

Premium
2:57

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

V
Vadim ✓ Link copied!

How does the "wasChanged" works under the hood? The model should be edited and saved to DB through Laravel, for this functionality to work or data in DB can be chaged from outside and this method will still know about the change?

How long this "Changed" status is present. I mean if model changed in the morning, and we'll check it in the evening - will it tell us 'true'? Or it's just instant - for the current code run?

It's kind of interesting functionality, however what might be real-world usage for it? Just wonder who needs it?

PK
Povilas Korop ✓ Link copied!

This is only for ONE request. It's a rare case indeed, mostly if many pieces of code may change the model and in your piece of code you want to find out whether someone else changed the model or not.

M
MimisK ✓ Link copied!

Hello.

Could you do a real example with forms, StoreRequests, etc. and put them in real life? Like "FirstOrCreate", "FirstOrNew" and "updateOrCreate"?

thnx

PK
Povilas Korop ✓ Link copied!

I think I've done it multiple times on YouTube, just search for "firstOrCreate" or "updateOrCreate" on my channel. "FirstOrNew" I don't remember using myself.

Found one: https://laraveldaily.com/video/eloquent-upsert-vs-updateorcreate-practical-demo

KV
Kailash Vele ✓ Link copied!

hello, can we check wasCreated function on UpdateorCreated function also?

PK
Povilas Korop ✓ Link copied!

Good question. I don't remember trying, but you should try and see.