首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以laravel为单位计算累积值

以laravel为单位计算累积值
EN

Stack Overflow用户
提问于 2017-04-03 21:09:10
回答 2查看 1.4K关注 0票数 1

我正在寻找一种方法来计算在laravel数据库的累积值。

目前,我检索一个值集合,每天一个。我希望能够在页面上显示累积值。我想要这样的东西

我的控制器中的代码是:

在这里,我从形式中找回制造和架设的价值。我想要计算每天的制造和架设的累积价值...

EN

回答 2

Stack Overflow用户

发布于 2017-04-03 21:13:28

您可以简单地在视图中执行此操作:

代码语言:javascript
运行
复制
<?php $cumulative = 0; ?>

@foreach ($collection as $object)
    <?php $cumulative += $object->value; ?>
    {{ $object->value }} | {{ $cumulative }}
@endforeach
票数 2
EN

Stack Overflow用户

发布于 2017-12-13 15:21:51

创建一个函数,然后执行该函数

代码语言:javascript
运行
复制
public function CumulativeCalculator($accounts){

    $driverAccounts     = [];
    $cumulativePayments = 0;
    foreach($accounts as $account){

        $cumulativePayments += $account->value;
        $currentDriverAccounts  = [];
        $currentDriverAccounts['id'] = $account->id;
        $currentDriverAccounts['realValue'] = $account->value;
        $currentDriverAccounts['cumulativeValue']  = $cumulativePayments;
        array_push($driverAccounts,$currentDriverAccounts);
    }
    return $driverAccounts;

}

在您的视图中执行此操作

代码语言:javascript
运行
复制
<table class="table table-hover table-striped">
    <thead>
        <th>Values</th>
        <th>Cumulative Values</th>
    </thead>
    <tbody>
        @foreach($driverAccounts as $dataValue)
            <tr>
                <td>{{$dataValue['realValue']}}</td>
                <td>{{$dataValue['cumulativeValue']}}</td>
            </tr>
        @endforeach
    </tbody>
</table>

这对你很管用,很抱歉我很着急,但它会管用的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43185507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档