fico7489
fico7489

Reputation: 8560

Laravel - How to get a date when some attribute/field had changed to some value?

For example I have table

orders 

and model

Order

order have attribute status. Initial status is 'cart', but second status is 'ordered', after that status is 'processed', after that status id 'delivery', after that status is 'canceled' or completed. I will probably have more than 10 statuses.

Is there any a better solution rather than creating datetime attribute/field for each status :

cart_at
ordered_at
processed_at
....

and then save datetime for each status after is changed ?

I want a package where I can do something like :

echo 'Order is cancelled at '. $order->getDateFor('status', 'canceled');

Upvotes: 0

Views: 146

Answers (2)

fico7489
fico7489

Reputation: 8560

You can use VentureCraft/revisionable package to track all changes on your model atributes and then use revisionable upgrade package https://github.com/fico7489/laravel-revisionable-upgrade to get date when status was updated :

echo 'order is updated to status "canceled" at ' . $order->dateUpdated('status', 'canceled');

Upvotes: 0

Mirdrack
Mirdrack

Reputation: 790

You can have a table name order_events an log there what is going on with your orders with the following fields:

id
order_id
event_type_id
details
created_at

Upvotes: 4

Related Questions