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

eristic's avatar

How to check if a row has been updated

I've got an edit view that takes a few variables from a controller. One variable in particular:

$event = Event::find($event_id);

      if($event->all_events) {
        $occurrences = $this->event->getAllEventsFromId($event_id);
      } else {
        $occurrences = $this->event->createEventsBasedOnId($event_id);
      }

is crucial to the view, but since it's dependent on a row existing, it retrieves only the first one, if it exists.

Obviously, this code is working as expected.

However, my question: Can I check is a row has been updated?

What's not working is isDirty() because the model has already been updated.

Is there a timestamp on each row? How would I access it?

Many thanks!

0 likes
5 replies
jasrys's avatar

Does your migration for the Event model have a $table->timestamps() or $table->nullableTimestamps() method call in it? If so, you may access the timestamps like so:

$event  = Event::find($event_id);

echo $event->updated_at;

//also, there's:

echo $event->created_at;

If you don't have those in your Event migration, you can add them easily following the instructions here. Create a new add_timestamps_to_events_table migration with php artisan make:migration add_timestamps_to_events_table --table=events and something like this ought to do the trick:

Schema::table('events', function (Blueprint $table) {
    $table->timestamps();
});

Run the migration with php artisan migrate and you'll have your created_at and updated_at fields on your events. Nothing to do in your controllers when saving/creating...Laravel handles setting these fields automatically out of the box.

eristic's avatar

Many thanks, @jasrys I should have phrased it better. I'm looking for one column being updated based on ID, not row. The column all_events in this case.

Here's pseudo code:

if($events->('all_events') == updated) {

} else {

}

Thanks!

jasrys's avatar

As far as I know, a single column in a row cannot have its own timestamp. What does all_events represent in your application? It seems a bit odd to me that an Event model would have its own all_events property. Maybe some DB restructuring can get you to what you need. Are you trying to represent something like:

$woodstock = Event::where('name', 'Woodstock')->first();

dd($woodstock->occurrences);

[
    'Woodstock 1969',
    'Woodstock 2016',
]
jekinney's avatar

You have access to model events. Few are updating and updated. Also on update it does set a new updated at timestamp, but if it's been updated before it won't match the created at anyways.

Jeffery does have an older series about tracking updates and what's been updated. Some of the techniques might be useful.

eristic's avatar

@jekinney Thank you for the feedback, a couple months later. Just to give an update, the client was fine with not needing this as a solution.

I ended up grabbing all the data as JSON and saving it in a database row and taking it out later to use it. It was sort of reverse engineer, but, it works and the client is happy, so who knows?

1 like

Please or to participate in this conversation.