How to delete all Eloquent Records in a database where the created_at or updated_at field is older than 60 minutes? (or any amount of time for example)
Resolved → With his ceejayoz link
- Make an object date and minus the amount of time.
- Hide date object to string to match Eloquent formatting
- Compare formatted string with updated_at
I am using the following code:
$date = new DateTime;
$date->modify('-60 minutes');
$formatted = $date->format('Y-m-d H:i:s');
MyModel::where('updated_at', '<=', $formatted)->delete();
Thanks ceejayoz!
source
share