如何计算Laravel表中的子总数和总数?

时间:2018-08-11 14:41:38

标签: laravel laravel-5 eloquent laravel-blade laravel-5.6

如果我雄辩地调试了laravel,结果如下:

SELECT a.transaction_number a.date, a.item_number, b.desc, a.variant_code, sum(a.quantity) AS quantity, a.cost
FROM `items_details` AS a
JOIN `items` AS b ON b.id = a.item_number
WHERE a.item_number = 0101010
GROUP BY a.variant_code
ORDER BY transaction_number, variant_code

我将分页返回收藏集

但是这里我只显示数组。像这样的数组:

$data = array(
    array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '002', 'quantity' => '2','cost' => '2000'),
    array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '004', 'quantity' => '3','cost' => '2000'),
    array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '005', 'quantity' => '4','cost' => '2000'),
    array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '006', 'quantity' => '5','cost' => '2000'),
    array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '008', 'quantity' => '1','cost' => '2000'),
    array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '013', 'quantity' => '2','cost' => '2000'),
    array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '020', 'quantity' => '3','cost' => '2500'),
    array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '022', 'quantity' => '4','cost' => '2500'),
    array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '007', 'quantity' => '1','cost' => '2500'),
    array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '015', 'quantity' => '7','cost' => '2500')
);

我在laravel刀片中的脚本显示如下:

<table class="table">
    <tr>
        <th>transaction_number</th>
        <th>date</th>
        <th>item_number</th>
        <th>desc</th>
        <th>variant_code</th>
        <th>quantity</th>
        <th>cost</th>
    </tr>
    @foreach ($items as $item)
        <tr>
            <td>{{ $item->transaction_number }}</td>
            <td>{{ $item->date }}</td>
            <td>{{ $item->item_number }}</td>
            <td>{{ $item->desc }}</td>
            <td>{{ $item->variant_code }}</td>
            <td>{{ $item->quantity }}</td>
            <td>{{ $item->cost }}</td>
        </tr>
    @endforeach
</table>

结果如下:

enter image description here

我想要这样的结果:

enter image description here

最好的方法是什么?是否通过mysql查询或laravel雄辩地显示那样?还是通过观察刀片的幼虫布置的?

请帮帮我。我很困惑:)

2 个答案:

答案 0 :(得分:1)

没有分页,我会做这样的事情(未经测试)...

要求分页会增加可能分解交易组和总数的复杂性。

已阅读以下内容,
https://laravel.com/docs/5.6/blade#the-loop-variable
https://laravel.com/docs/5.6/blade#including-sub-views

$loop需要Laravel 5.3 +。

subtotal.blade.php

<tr>
    <td colspan="4"></td>
    <td>SUBTOTAL</td>
    <td>{{ $items->where('transaction_number', $current_transaction_number)->sum('quantity') }}</td>
    <td>{{ $items->where('transaction_number', $current_transaction_number)->sum('cost') }}</td>
</tr>
<tr>
    <td colspan="7"></td>
</tr>

total.blade.php

<tr>
    <td colspan="4"></td>
    <td>TOTAL</td>
    <td>{{ $items->sum('quantity') }}</td>
    <td>{{ $items->sum('cost') }}</td>
</tr>

main.blade.php

<table class="table">
    <tr>
        <th>transaction_number</th>
        <th>date</th>
        <th>item_number</th>
        <th>desc</th>
        <th>variant_code</th>
        <th>quantity</th>
        <th>cost</th>
    </tr>
    @php ($current_transaction_number = null)

    @foreach ($items as $item)
        @if ($loop->index > 0 && $current_transaction_number != $item->transaction_number)
           @include ('subtotal', compact('items', 'current_transaction_number'))
        @endif
        <tr>
            @if ($current_transaction_number == $item->transaction_number)
                <td colspan="2"></td>
            @else
                @php ($current_transaction_number = $item->transaction_number)
                <td>{{ $item->transaction_number }}</td>
                <td>{{ $item->date }}</td>
            @endif
            <td>{{ $item->item_number }}</td>
            <td>{{ $item->desc }}</td>
            <td>{{ $item->variant_code }}</td>
            <td>{{ $item->quantity }}</td>
            <td>{{ $item->cost }}</td>
        </tr>
        @if ($loop->last)
           @include ('subtotal', compact('items', 'current_transaction_number'))
           @include ('total', compact('items'))
        @endif
    @endforeach
</table>

{{ $items->links() }}

答案 1 :(得分:0)

@php
    $previous_transaction_number = "";
    $total_quantity = 0;
    $total_cost = 0;
    $subtotal_quantity = 0;
    $subtotal_cost = 0;
@endphp

<table class="table" border='1'>
    <tr>
        <th>transaction_number</th>
        <th>date</th>
        <th>item_number</th>
        <th>desc</th>
        <th>variant_code</th>
        <th>quantity</th>
        <th>cost</th>
    </tr>
    @foreach ($items as $item)
        @if(!empty($previous_transaction_number) && $previous_transaction_number != $item['transaction_number'])
            <tr>
                <td colspan="5" style="text-align:right"><b>SUBTOTAL:</b></td>
                <td><b> {{ $subtotal_quantity }} </b></td>
                <td><b> {{ $subtotal_cost }} </b></td>
            </tr>
            @php
                $subtotal_quantity = 0;
                $subtotal_cost = 0;
                $previous_transaction_number = "";
            @endphp
            <tr>
                <td colspan="7">&nbsp;</td>
            </tr>
        @endif

        <tr>
            @if($previous_transaction_number != $item['transaction_number'])
                <td>{{ $item['transaction_number'] }}</td>
            @else
                <td></td>
            @endif            
            <td>{{ $item['date'] }}</td>
            <td>{{ $item['item_number'] }}</td>
            <td>{{ $item['desc'] }}</td>
            <td>{{ $item['variant_code'] }}</td>
            <td>{{ $item['quantity'] }}</td>
            <td>{{ $item['cost'] }}</td>
        </tr>

        @php
            $subtotal_quantity += $item['quantity'];
            $subtotal_cost += $item['cost'];
            $previous_transaction_number = $item['transaction_number'];
            $total_quantity += $item['quantity'];
            $total_cost += $item['cost'];
        @endphp        
    @endforeach
    <tr>
        <td colspan="5" style="text-align:right"><b>SUBTOTAL:</b></td>
        <td><b> {{ $subtotal_quantity }} </b></td>
        <td><b> {{ $subtotal_cost }} </b></td>
    </tr>

    <tr>
        <td colspan="7">&nbsp;</td>
    </tr>

    <tr>
        <td colspan="5" style="text-align:right"><b>TOTAL:</b></td>
        <td><b> {{ $total_quantity }} </b></td>
        <td><b> {{ $total_cost }} </b></td>
     </tr>

</table>