【发布时间】:2021-10-14 08:57:36
【问题描述】:
我有一个显示分组数据的表格,并有超链接形式的“按钮”,供用户查看有关整个分组数据的更多信息。但是,该按钮也会随着每一行循环,理想情况下我只希望它为每个组显示一次。我尝试在 @foreach 循环中移动,但不断收到请求的 ID 在此集合实例中不存在的错误。
例如,我试图让这些链接在每个组中只出现一次(如图所示,在 1 和 2 中出现一次)。
代码:
<tr>
@foreach ($grouped as $mealplan)
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Recipe Information
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Meal Plan Info
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Print
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Archive
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Recipe Name
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Day
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach ($mealplan as $recipe)
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="recipeinformation/{{$recipe->Recipe_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">More</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="/MealPlanDisplay/modal/{{$recipe->MealPlan_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Meal Plan Info</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="printpreviewingredients/{{$recipe->MealPlan_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Print Ingredient List</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="/Archive/{{$recipe->MealPlan_ID}}" class="text-red-600 hover:text-indigo-900 mb-2 mr-2">Archive</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
{{$recipe->recipe_name}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
{{$recipe->Day}}
</td>
</tr>
</br>
@endforeach
@endforeach
任何关于将循环移动到哪里的建议,或者如何让链接在表格中的每个组中只显示一次,我们将不胜感激。
【问题讨论】:
-
你能不能不使用laravel.com/docs/8.x/blade#the-loop-variable 并检查它是否是foreach 中的第一次迭代。如果是,则显示更多视图,如果不是,则显示其他内容或将其替换为