如何传递变量到视图并修复查询laravel

1szpjjfi  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(277)

我得到了一个表单和一个数据库mysql,我试图做一个查询,显示这些表单提交的报告。在我面临的这个问题中,发生的事情是,只要我添加另一个carplate,并提交一些表单,它不会很好地进行计算,我希望它被carPlate和totalKm分组,显示每个carplate的简历,只给出每个汽车牌照的总和,我注意到的是,一些总和是正确的,但没有很好地分组或查询..
//刀片

<tr>
                            @foreach ($queryFinal as $item)
                            <td>{{ $item->carplate }}</td>
                            <td>{{ $item->totalKMs }}</td>
                            <td>{{ $item->average100 }}</td>
                            <td>{{ $item->totalEurosCharged }}</td>
                            <td>{{ $item->totalrep }}</td>
                            <td>{{ $item->totalPorts }}</td>
                            <td>{{ $item->costKM }}</td>
                        </tr>
                        @endforeach
                    </tbody>

//控制器

public function index(Request $request, Repair $valor, Form $kilometrage, Ports $cost)
    {      
        $totalLitersCharged = $kilometrage->sum('liters_charged');
        $totalEurosCharged = $kilometrage->sum('euros_charged');
        $totalrep = $valor->sum('repair.valor');
        $endKmSum = $kilometrage->sum('endkm');
        $totalrep = $valor->sum('repair.valor');
        $currentKilometrage = $kilometrage->sum('kilometrage');
        $cost = $cost->sum('cost');
        $portsEuro = $kilometrage->sum('portsEuro');
        $queryFinal = DB::table("form")
            ->selectRaw("form.carPlate,(form.endKm-form.kilometrage) AS totalKm,(100*SUM(liters_charged))/SUM(form.endKm-form.kilometrage) AS average100,$totalEurosCharged as totalEurosCharged , $totalrep as totalrep  , $cost + $portsEuro as totalPorts, $totalEurosCharged + $totalrep + $portsEuro + $cost / (form.endKm-form.kilometrage) AS costKM")
            ->leftJoin('repair', 'repair.carPlate', '=', 'repair.carPlate')
            ->leftJoin('ports', 'ports.carPlate', '=', 'ports.carPlate')
            ->orderBy("form.carPlate")
            ->groupBy("carPlate","form.endKm","kilometrage")
            ->get();
        return view('admin.form.report', compact('queryFinal','totalrep','cost','portsEuro','totalEurosCharged'));
}
zpgglvta

zpgglvta1#

<tr>
    @foreach ($queryFinal as $item)
        <td>{{ $item->carplate }}</td>
        <td>{{ $item->totalKMs }}</td>
        <td>{{ $item->average100 }}</td>
        <td>{{ $item->totalEurosCharged }}</td>
        <td>{{ $item->totalrep }}</td>
        <td>{{ $item->totalPorts }}</td>
        <td>{{ $item->costKM }}</td>
    @endforeach
</tr>  <---- here

像这样试试看

相关问题