Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda