5

I have this query in Laravel Eloquent:

$measures = Measure::groupBy('time')
        ->selectRaw('time, sum("delta") as sum_delta, sum("redistributed") as sum_redistr') 
        ->where('operation_id', 'ACC0000001') 
        ->where('time', '>', '2020-05-09 00:00') 
        ->where('time', '<', '2020-05-10 00:00') 
        ->where('source', 'source1') 
        ->where('conso_prod', 'Conso')
        ->get()

When I debug using toSql() function, and then I paste it into pgAdmin, I get the correct result.

select time, sum("delta") as sum_delta, sum("redistributed") as sum_redistr from "measures" 
where "operation_id" = 'ACC0000001'
and "time" > '2020-05-09' and "time" < '2020-05-10' and "source" = 'source1' and "conso_prod" = 'Conso' group by "time"

And I have a result each 30m which is correct.

But when I use eloquent, I have the same amount of rows, but all the time fields are the same:

"2020-05-09 00:00"

instead of incrementing.

I don't understand why ? I use PostgreSQL with TimescaleDB extension.

Juliatzin
  • 18,455
  • 40
  • 166
  • 325
  • 1
    Quick question; is `time` a column on your `measures` table or a PGSql keyword? Sometimes when you use `selectRaw()` you need to use tildes around columns (like time) to avoid that. That being said, that's for MySQL; I don't work much with PGSql, so I can't say if that's an issue or not. – Tim Lewis Jul 29 '20 at 13:53
  • is the column 'time' holds a time value or dateTime value? – OMR Jul 29 '20 at 13:55
  • time is a column that has a timestamp value. It is also a special column, as TimescaleDB is a time series DB. – Juliatzin Jul 29 '20 at 14:07

1 Answers1

4

In my model, I had my time casted to date instead of datetime

protected $casts = [
    'time' => 'date'
];

I changed it to :

protected $casts = [
    'time' => 'datetime'
];

And it worked.

I let it there if someones fails like me ! Thanks for your help anyway

Juliatzin
  • 18,455
  • 40
  • 166
  • 325