0

We want to count the duration between two different date fields on a database. Can anyone help convert this SQL query to a database migration in Laravel?

CREATE TABLE api_incident_duration 
(START DATETIME, END DATETIME, elapsed DECIMAL(5, 2) 
AS (ROUND(TIMESTAMPDIFF(MINUTE, START, END) / 60, 2)));

Perhaps:

$table->dateTime('startdatetimepicker')->nullable();
$table->dateTime('enddatetimepicker')->nullable();
$table->decimal('elapsed', 5, 2)
   ->virtualAs(ROUND(TIMESTAMPDIFF(MINUTE, startdatetimepicker, enddatetimepicker) / 60, 2));
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • This is not valid PHP code. Do you mean `->virtualAs('ROUND(TIMESTAMPDIFF(MINUTE, startdatetimepicker, enddatetimepicker) / 60, 2)')`? – Jonas Staudenmeir Nov 26 '18 at 12:14
  • yes my friend, it worked ! thank you very much ! –  Nov 26 '18 at 12:15
  • 1
    @tphil Just for information , virtualAs is migration column modifiers. To know more : https://laracasts.com/discuss/channels/laravel/migration-column-modifiers. – Kul Nov 26 '18 at 12:18

0 Answers0