Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterpan26's avatar

how do i do this in query

hi, i got a query but it's not summing well the "kmsTotais" , how do i fix this ? and make it to work? i yesterday asked how to do the sum and subtraction in elloquent to then try to pass it onto the selectraw like i did with abastecimento, but it didnt work, please help fix this , thanks

 $totalEurosAbastecimento = $quilometragem->sum('abastecimento_euros');
        $totalLitrosAbastecimento = $quilometragem->sum('abastecimento_litros');
        $totalrep = $valor->sum('reparacoes.valor');
        $data = DB::table("reparacoes")
        ->select(Reparacoes::raw("reparacoes.matricula,data_ne, $totalLitrosAbastecimento as totalLitrosAbastecimento, 
        $totalEurosAbastecimento AS totalEurosAbastecimento, $totalrep as totalrep,SUM(formulario.kmfim-formulario.quilometragem) AS kmsTotais"))
            ->leftJoin('formulario', 'reparacoes.matricula', '=', 'formulario.matricula')
            ->leftJoin('viaturas', 'reparacoes.matricula', '=', 'viaturas.matricula')
            ->orderBy("reparacoes.matricula")
            ->groupBy(DB::raw("reparacoes.matricula"))
            ->get();
//blade
 @foreach ($data as $item)
                        <tr>
                            <td>{{ $item->matricula }}</td>
                            <td>{{ $item->totalrep }}</td>
                            <td>{{ $item->data_ne }}</td>
                            <td>{{ $item->kmsTotais }}</td>
                       
                            <td>{{ $item->totalLitrosAbastecimento }}</td>
                            <td>{{ $item->totalEurosAbastecimento }}</td>
                           
                        </tr>
                        @endforeach
                  note: quilometragem exists both in formulario ,viaturas and reparacoes table      
0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

First, this Raw expression

->select(Reparacoes::raw("reparacoes.matricula,data_ne, ...

should be written as

->selectRaw("reparacoes.matricula,data_ne, ...

Why is it necessary to interpolate the $totalLitrosAbastecimento and $totalrep into the query expression?

peterpan26's avatar

@tykus the $totalLitrosAbastecimento and $totalrep are elements that i need to sum, and before when i tried without elloquent and pass them like this it was not summing well the records, now it is like this, but the kmsTotais keeps the same result as before with the old raw expression i tried now changing and it didnt make any difference, but thanks, it is summing 10x at the moment instead of 1x if that can help. it was supposed to give a result of 25 and its giving 225

peterpan26's avatar

i already fixed it by making a separate query in the same function and then call it on blade on a different foreach i know its not the way of resolving a problem like this once and forall (joins problem) but it fixes temporarily the problem

Please or to participate in this conversation.