Delete Elequent records older than 60 minutes Delete Elequent records older than 60 minutes database database

Delete Elequent records older than 60 minutes


Laravel have a build in Carbon date class which handles all the timezone problems for you.

Example:

$date  = Carbon::now()->subMinutes( 60 );MyModel::where( 'updated_at', '<=', $date )->delete();


Solved -> With ceejayoz his link

  • Make a date object and minus the amount of time.
  • Covert the date object to string to match Eloquent formatting
  • Compare formatted string against updated_at

I'm 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!