jswoolf01's avatar

How does wasChanged work with related tables?

I'm writing some complex validations for an inventory table - a table of products. This Products table has a couple of related child tables. I'd like to save some resources by only running validations for fields that have changed.

Scenario: I edit Product A, change a couple of fields in the Product record and also change some of the data that's stored in one of the child tables. Next, I edit Product B and only change child-table data; the Product record isn't changed at all.

What will wasChanged() show for Product A? What about for Product B?

0 likes
3 replies
Glukinho's avatar

What will wasChanged() show for Product A? What about for Product B?

How about just try it out yourself? In tinker or some test artisan command.

1 like
jswoolf01's avatar

Because I've had it happen in the past that I tried something myself, thought I had it right, then later discovered there was some aspect of it that I didn't know about, which resulted in my code working as intended sometimes and not working sometimes, and figuring out exactly what was going on and how to fix it was hellish. Worse, on at least one occasion I discovered that there was no way to fix it; I had to throw out several days worth of work and start over from scratch.

JussiMannisto's avatar

Please or to participate in this conversation.