Php 返回按集合中的值分组的数组总和的雄辩查询

Php 返回按集合中的值分组的数组总和的雄辩查询,php,mysql,eloquent,twig,Php,Mysql,Eloquent,Twig,我正在构建一个应用程序,它使用slim、twig和雄辩的语言。在我的一个页面上,我展示了一组项目,首先分成两组,然后再按类别进一步分成一组。每件物品都有重量。我正在输出第一次拆分的所有项目的总重量。我从第二次拆分中输出每个类别的名称一次。现在我只想得到该类别中项目的总重量,并将其列在该类别名称下 这是从路线: $userId = $app->auth->id; $collection = collect($app->item->where('user_id', $use

我正在构建一个应用程序,它使用slim、twig和雄辩的语言。在我的一个页面上,我展示了一组项目,首先分成两组,然后再按类别进一步分成一组。每件物品都有重量。我正在输出第一次拆分的所有项目的总重量。我从第二次拆分中输出每个类别的名称一次。现在我只想得到该类别中项目的总重量,并将其列在该类别名称下

这是从路线:

$userId = $app->auth->id;

$collection = collect($app->item->where('user_id', $userId)->get()); // All items from the current user

$totalWeight = $collection->sum('grams');

$pack = $collection->filter(function($gear) { // All items with status 1 from the current user
    if ($gear->status === 1) {
        return true;
    }
})->sortBy('category');

$storage = $collection->filter(function($gear) { // All items with status 0 from the current user
    if ($gear->status === 0) {
        return true;
    }
})->sortBy('category');

$app->render('user/gear.php', [
    'pack' => $pack,
    'storage' => $storage,
    'totalWeight' => $totalWeight
]);
这是我的观点:

<div class="pack">
    <header class="pack__header">
        <h2 class="pack__header__title">Backpack</h2>
        <span class="pack__header__weight">Total Weight: {{ totalWeight|outputWeights(totalWeight) }}</span>
    </header>

    {% set currentCategory = null %}
    {% for item in pack %}
        {% if item.category != currentCategory %}
            <h3 class="categoryName">{{ item.category|getCatName(item.category) }}</h3>
            {% set currentCategory = item.category %}
        {% endif %}

    <div class="item">
        <ul class="item__lineOne">
            <input type="checkbox" form="itemCheck" name="ID of the item" value="selected">
            <li class="item__lineOne__name">{{ item.name }}</li>
            <li class="item__lineOne__weight">{{ item.grams }}</li>
        </ul>
        <div class="collapse">
            <ul class="item__lineTwo">
                <li class="item__lineTwo__description">{{ item.description }}</li>
            </ul>
            <ul class="item__lineThree">
                <li class="item__lineThree__url">
                    <a class="item__lineThree__url__link" href="{{ item.url }}">{{ item.url }}</a>
                </li>
            </ul>
            <button type="button" class="modifyItemButton">Modify</button>
        </div>
    </div>
    {% endfor %}
</div>

背包
总重量:{{totalWeight}输出重量(totalWeight)}
{%set currentCategory=null%}
{对于包%中的项目,%}
{%if item.category!=currentCategory%}
{{item.category}getCatName(item.category)}
{%set-currentCategory=item.category%}
{%endif%}
  • {{{item.name}
  • {{{item.grams}
  • {{{item.description}
修改 {%endfor%}
如果我需要在视图中的foreach期间使用一些代码,我还有一个带有一些Twig_simplefilter的文件。我只是不知道在哪里或者什么是解决这个问题的有效方法

  • 您可以简化收集方法:

    $pack = $collection->where('status', 1)->sortBy('category');
    
    而不是过滤器

  • 您不需要排序,请改用
    groupBy

    $pack = $collection->where('status', 1)->groupBy('category');
    
    然后在模板中为每个类别使用
    sum

    {% for category,items in pack %}
      <h3 class="categoryName">{{ category|getCatName(item.category) }}
      <br>weight: {{ items.sum('grams') }}
      </h3>
    
      {% for item in items %}
        <div class="item"> ... </div>
      {% endfor %}
    {% endfor %}
    
    {%用于类别,包装中的项目%}
    {{category | getCatName(item.category)}
    
    重量:{{items.sum('grams')} {items%%中的项的%s} ... {%endfor%} {%endfor%}

  • 这工作做得很好!我从未见过将多个数组传递到一个for中,也不知道如果没有在路由中明确命名,该组会成为这样的目标。去研究文件。谢谢