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

peterpan26's avatar

QUERY RESULTS FETCH ON LARAVEL

hi, i got the two following tables table name : formulario id primary bigint(20) mes_ano varchar(255) matricula varchar(255) table name: reparacoes id primary bigint(11) matricula varchar(255) valor varchar(255) what i want to accomplish is joining on the valor with formulario's values and fetch the data in laravel view dashboard. i tried the following way, but with the join it doesnt show accurate values.

public function index(Request $request, Formulario $id, Reparacoes $valor)
    {
        $data = Reparacoes::select(DB::raw("SUM(reparacoes.valor) as sum"))
        ->join('formulario', 'reparacoes.matricula', '=', 'formulario.matricula')
        ->orderBy("reparacoes.matricula")
        ->groupBy(DB::raw("reparacoes.matricula"))
        ->get();

        return view('admin.page', ['data' => $data]);
    }
 <tbody>
                        @foreach ($data as $item)
                        <tr>
                            <td>{{ $item->matricula }}</td>
                            <td>{{ $item->sum }}</td>
                            <td>{{ $item->mes_ano }}</td>
                        </tr>
                        @endforeach     
                    </tbody>

i don't even put on the select the mes_ano because the sum it's not working i got two repairs on one matricula that summing up gives 30 in value, and the other matricula that have reparacao has just one of 10 when i apply the join in controller, it shows up as the first matricula having a sum of 420. thanks for whoever helps me doing this join query sum.

0 likes
0 replies

Please or to participate in this conversation.