1

I would like to select records from a MySQL table with Laravel where NOW() is later than a datetime-field plus the value of an integer-field (seconds). - How can I do this?

Table "records"

  • id (int)
  • name (varchar)
  • updated_at (datetime)
  • update_interval (int)

I wish to accomplish something like:

SELECT * FROM records WHERE NOW() > (updated_at + update_interval);

And then written as a Laravel where-condition like:

Record::where( 'NOW()', '>', 'created_at + update_interval' );
preyz
  • 3,029
  • 5
  • 29
  • 36

1 Answers1

0

You probably want to compute a time. Adding a number to a DATETIME column reduces it to a numerical value.

Instead use DATE_ADD to keep it as the proper type:

Record::where('NOW()', '>', 'DATE_ADD(created_at, INTERVAL update_interval SECOND)')

You can adjust the interval computation here by using different units. Seconds offer the most control, but lead to large, confusing to read numbers like 86400.

tadman
  • 208,517
  • 23
  • 234
  • 262