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

164
Views
Laravel9 - selectRaw sum doubling the result error

I have 5 tables I want to join

Invoices    Coletes   Clients  TagaCars  Incasaris
id 1           1         1         1         1 
               2
               3     

My query is: summing all the child coletes sum where coletes.invoice_id = invoices.id selecting the clients name selecting the tagacars model BUT is supposed to sum all the incasaris(which is basically encashment of an invoice), but when I do the sum in query it is doubling the sum for no reason

This is my query:

$invoices = Invoice::join('clients','clients.id','=','invoices.client_id')
->join('taga_cars','taga_cars.id','=','invoices.car_id')

->leftJoin('coletes','coletes.invoice_id','=','invoices.id')
->leftjoin('incasaris','invoices.id','=','i_plati_id')
->selectRaw(' 
sum(coletes.totaleuro) as total,
sum(coletes.totaleuro * invoices.i_rate) as totalabsolut,
sum(incasaris.i_totallei) as totalincasat,
clients.name, invoices.*, taga_cars.model')
->groupBy('invoices.id')
->orderBy('invoices.id')
->get();

The problem is only with "totalincasat"

Here you can see in my sql the ammount of i_totallei for one of my invoices

and here is my DD which is showing the doubled ammount for no reason

I want to mention that at the moment each invoice has only one incasaris belonging to each, and also, all incasaris summed combined would not be summed as 2000, it is simply doubling the result Does anyone have any suggestion? Is my first time I got to this "error" and I don't seem to understand how to fix it.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Please check leftjoin of invices with incasaris, you are missing incasaris.i_plati_id in bellow mentioned line:

->leftjoin('incasaris','invoices.id','=','i_plati_id') 

Instead of above, it must be like

->leftjoin('incasaris','invoices.id','=','incasaris.i_plati_id')
about 4 years ago · Santiago Gelvez 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!