【问题标题】:How to show the total on the last rows with same value? (Laravel)如何显示具有相同值的最后一行的总数? (拉拉维尔)
【发布时间】:2021-10-06 17:07:24
【问题描述】:

我的 dtaabse 中有表“transaction_items”,看起来与此类似...

id transaction_id qty unit_price
1 100 1 10.00
1 100 2 10.00
1 100 3 10.00
1 200 1 20.00
1 200 1 20.00
1 300 1 15.00

当前我获取数据: $data['transaction_items'] = DB::table('transaction_items')->get();

并运行一个 foreach 循环:

foreach ($transaction_items as $trans) {
 <tr>
  <td> {{ $trans->transaction_id}} </td>
  <td> {{ $trans->qty}} </td>
  <td> {{ $trans->unit_price}} </td>
  <td> * WHOLE TRANSACTION TOTAL</td>

这会像您期望的那样完美地输出数据。虽然我输出的最后一个值 (*WHOLE TRANSACTION TOTAL) 正在运行另一个查询和 foreach 循环以获取整个事务的“总数”。

<tr>
 <td>
  <?php 
  $totals = DB::table('transaction_items')->select('qty', 'unit_price')->where('transaction_id', '=', $trans->transaction_id)->get();
  $ftot = 0;
  foreach ($totals as $total) {
    $ftot += $total->qty * $total->unit_price;
  }
  echo '&pound;'.number_format($ftot, 2);
  ?>
 </td>
</tr>

我知道这可能是错误的做法,但这就是我的原因。

我当前的输出看起来像这样......

transaction_id qty unit_price Trans Total
100 1 10.00 60.00
100 2 10.00 60.00
100 3 10.00 60.00
200 1 20.00 40.00
200 1 20.00 40.00
300 1 15.00 15.00

我希望它看起来像这样,它显示具有相同 transaction_id 的最后一行之后的总数。

transaction_id qty unit_price Trans Total
100 1 10.00
100 2 10.00
100 3 10.00 60.00
200 1 20.00
200 1 20.00 40.00
300 1 15.00 15.00

希望这是有道理的,并提前感谢。

额外编辑

我曾尝试使用“减少”方法,但得到...

$totals = DB::table('transaction_items')
    ->select('qty', 'unit_price')
    ->where('transaction_id', '=', $trans->transaction_id)
    ->get()
    ->groupBy('transaction_id')->each(function($item) {
    $sum = $item->reduce(fn($carry, $product) => $carry + ($product['qty'] * $product['unit_price']));
    $lastRow = $item->pop();
    $lastRow['sum'] = $sum;
    $item->push($lastRow);
    return $item;
})->flatten(1);

$ftot = 0;     
foreach ($totals as $total) {
 $ftot += $total->qty * $total->unit_price;
} 
echo '&pound;'.number_format($ftot, 2);

但我收到此错误: Cannot use object of type stdClass as array

如果我在 original $totals 上执行“print_r”,这是其中一行的示例..

Illuminate\Support\Collection Object ( 
 [items:protected] => Array ( 
  [0] => stdClass Object ( 
   [transaction_id] => 20940 
   [qty] => 4.00 
   [unit_price] => 80.0000 
  ) 
  [1] => stdClass Object ( 
   [transaction_id] => 20940 
   [qty] => 6.00 
   [unit_price] => 10.0000 
  ) 
  [2] => stdClass Object ( 
   [transaction_id] => 20940 
   [qty] => 308.00 
   [unit_price] => 0.4000 
  ) 
  [3] => stdClass Object ( 
   [transaction_id] => 20940 
   [qty] => 308.00 
   [unit_price] => 0.0500 
  ) 
  [4] => stdClass Object ( 
   [transaction_id] => 20940 
   [qty] => 1.00 
   [unit_price] => 10.3000 
  ) 
 ) 
)

【问题讨论】:

    标签: php html sql laravel


    【解决方案1】:

    简单——Laravel collections 功能强大

    $total = $total->reduce(function ($carry, $item) {
        return $carry + ($item->qty * $item->unit_price);
    });
    

    或者内联(这段代码我没查过,可能不行)

    $total = $total->reduce(fn ($carry, $item) => $carry + ($item->qty * $item->unit_price));
    

    更新

    我没有正确阅读问题,抱歉。但是收藏仍然很强大。顺便说一句,我认为,您必须考虑这种行为 - 更好的方法是添加一个维度 - 按事务分组并为每个组添加小计

    $totals = DB::table('transaction_items')
        ->select('qty', 'unit_price')
        ->where('transaction_id', '=', $trans->transaction_id)
        ->get()
        ->groupBy('transaction_id')->each(function($item) {
            $sum = $item->reduce(fn($carry, $product) => $carry + ($product['qty'] * $product['unit_price']));
            $lastRow = $item->pop();
            $lastRow['sum'] = $sum;
            $item->push($lastRow);
            return $item;
    })->flatten(1);
    

    【讨论】:

    • tx 但我收到一个错误:不能使用 stdClass 类型的对象作为数组。在我的 index.blade 中
    • 我把你更新的部分放在'foreach($totals as $total)'循环中
    • @JasonF 不在 foreach 中。更新
    • Tx,但我的意思是说我添加到了我的 $totals 变量中。
    • 如果有帮助,我已经添加了一些额外的信息。它似乎是在 reduce 方法上抛出错误
    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2016-10-06
    • 2018-11-12
    • 1970-01-01
    • 2022-11-30
    相关资源
    最近更新 更多