Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!