【问题标题】:How to calculate total column value without query by Laravel or php?如何在不通过 Laravel 或 php 查询的情况下计算总列值?
【发布时间】:2020-11-27 08:19:11
【问题描述】:

在上面我想计算当月的买入和卖出总数。假设在 11 月我要计算卖出行总数、买入行总数和买卖总数。我怎样才能做到这一点 。请帮忙...

**<table id="datatable" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Date</th>
            <th>Product Name</th>
            <th>Quantity (Stock IN)</th>
            <th>Buying Total</th>
            <th>Stock</th>
            <th>Selling Total</th>
            <th>Profit (%)</th>

        </tr>
    </thead>


    <tbody>
        @foreach($product_all as $row)
        <tr>
            <td>{{ $row->buying_date}}</td>
            <td>{{ $row->product_name}}</td>
            <td>{{ $row->stock_in }}</td>
            <td>{{ $row->buying_price*$row->stock_in }}</td> *********
            <td>{{ $row->total_stock }}</td>
            
            <td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
            
            <td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>

        </tr>
       @endforeach
        
    </tbody>

</table>**



<h4 style="color: #ff3939; text-align: center;">November Total Invest : {{ #### }} <span> Sell : {{ #### }}</span></h4>

【问题讨论】:

  • 您的问题有两个答案。如果任何一个答案解决了您的问题,请记住标记为已接受,以使后续用户受益,尤其是新手,他们觉得接受的答案以首选方式解决了问题。如果答案不能解决您的问题,至少让答案作者知道您遇到了什么错误或为什么答案不能解决您的问题

标签: laravel count calculation


【解决方案1】:

有两种方法:

第一:设置参数

**<table id="datatable" class="table table-striped table-bordered">
    <thead>
        <tr>
            ...
        </tr>
    </thead>
    <tbody>
<?php 
$yoursum1 = 0;
$yoursum2 = 0;
        foreach($product_all as $row)
        echo "<tr>
            <td>{{ $row->buying_date}}</td>
            <td>{{ $row->product_name}}</td>
            <td>{{ $row->stock_in }}</td>
            <td>{{ $row->buying_price*$row->stock_in }}</td> *********
            <td>{{ $row->total_stock }}</td>
            <td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
            <td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>

        </tr>"
$yoursum1 += $yourvalue1;
$yoursum2 += $yourvalue2;
       endforeach
            echo "<tr>
                <td></td>
                <td>Summary</td>
                <td>{{ $yoursum1}}</td>
                <td>{{ $yoursum2}}</td> *********
                <td>{{ $yoursum3}}</td>
                <td>{{ $yoursum4}}</td> ********
                <td>{{ $yoursum5 }}</td>
    
        </tr>"
        ?>
    </tbody>

</table>**
<h4 style="color: #ff3939; text-align: center;">November Total Invest : {{ #### }} <span> Sell : {{ #### }}</span></h4>

第二种:使用查询计算并在下面插入一行

    @foreach($product_all as $row)
        <tr>
            <td>{{ $row->buying_date}}</td>
            <td>{{ $row->product_name}}</td>
            <td>{{ $row->stock_in }}</td>
            <td>{{ $row->buying_price*$row->stock_in }}</td> *********
            <td>{{ $row->total_stock }}</td>
            <td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
            <td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
        </tr>
       @endforeach
       <tr>
            <td></td>
            <td>Summary</td>
            <td>{{ $value1 }}</td>
            <td>{{ $value2 }}</td> *********
            <td>{{ $value3 }}</td>
            <td>{{ $value4 }}</td> ********
            <td>{{ ($value5 }}</td>
        </tr>

【讨论】:

    【解决方案2】:

    建议 IT 将计算和处理排除在视图之外。控制器是用来完成所有计算和数据操作的文件。

    //Say this is inside the controller method from which the view with data is returned as response
    
    public function index(Request $request)
    {
        $product_all = Product::all();
    
        $groupedByMonth = $product_all->groupBy(function($product) {
           return \Illuminate\Support\Carbon::parse($product->date)->format('m');
        });
    
        $totals = (object) [
            'buying' => $groupedByMonth->map(function($month){
                 return $month->sum('buying_total');
            }),
            'selling' => $groupedByMonth->map(function($month){
                 return $month->sum('selling_total');
            }),
        ];
    
        //Pass $totals to the view where it can be accessed as $totals->buying
    }
    

    因此,计算/操作可以在 @php @endphp 之间的刀片视图中完成

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2017-05-16
      相关资源
      最近更新 更多