Update At
Automatically manages updated_at timestamps when records are modified
The withUpdatedAt
mixin automatically updates timestamps when records are modified in the database. This ensures you always know when a record was last changed without manually setting the timestamp on every update.
Usage
API Reference
withUpdatedAt()
Enhances a model with automatic updatedAt timestamp functionality and adds a convenient updateById
method.
Parameters
model
- The base model to enhancefield
- The name of the field to use for update timestamps (e.g., 'updatedAt')
Returns
Enhanced model that automatically sets the update timestamp field during update operations and provides an updateById convenience method.
Features
- Automatic Timestamps: Sets the timestamp field to current date/time on every update
- Intercepts Set Method: Adds the timestamp to all update operations
- Direct Update Method: Provides a convenient
updateById
method - SQLite Compatibility: Formats dates appropriately for SQLite compatibility
Additional Methods
updateById()
A convenience method that updates a single column value by ID and automatically updates the timestamp.
Parameters
id
- The ID of the record to updatecolumn
- The column to updatevalue
- The new value for the column
Returns
The updated record with the new value and updated timestamp.