1
我正在構建一個應用程序,使用苗條,樹枝和雄辯。在我的其中一頁上,我展示了一組首先分成兩組的項目,然後再按類別進一步分組。每件產品都有重量。我正在輸出第一次拆分中所有項目的總重量。我從第二次拆分中輸出每個類別的名稱一次。現在我想獲得該類別中僅項目的總重量並將其列在該類別名稱下。雄辯的查詢返回一個數組的總和由一個collecion中的值分組
這是從路徑:
$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>
我也有一些Twig_SimpleFilters一個文件,如果我需要在在foreach期間使用一些代碼視圖。我只是不確定在哪裏或什麼是解決這個問題的有效方法。
這完美地工作!我從來沒有見過多個數組傳遞到一個單一的,並不知道該組可能是這樣的目標,沒有在路線中明確命名。關閉學習文檔。謝謝。 – user2530671