Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different result beween eloquent query and the same associated SQL query (got with toSql() method ) with Laravel

Tags:

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.

like image 431
Juliatzin Avatar asked Jul 29 '20 13:07

Juliatzin


1 Answers

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

like image 60
Juliatzin Avatar answered Oct 02 '22 15:10

Juliatzin