在foreach循环期间,您可以在视图中拆分表吗?

tvokkenx  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(328)

我正在做一个膳食计划项目,为了直观演示,我想知道在foreach循环中,每个具有唯一膳食计划id的表是否可以在每个具有显示数据的表之间有一个中断(分隔)?
目前,生成的每个新用餐计划只会添加到一个表中。
观点:

<thead>
    <tr>
        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
            ID
        </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>
        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
            View More
        </th>
    </tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
    @foreach ($data as $var)
        <tr>
            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                {{$var->Recipe_ID}}
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                {{$var->recipe_name}}
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                {{$var->Day}}
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <a href="recipeinformation/{{$var->Recipe_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">More</a>
            </td>
        </tr>
    @endforeach
</tbody>

我有两个影响表填充的数据库表,mealplan表和recipe表。
MealPlandSplayController中的索引方法:

public function index(){

$currentuserid = Auth::id();

$data = Recipe::join('mealplan_main', 'mealplan_main.Recipe_ID', '=', 'recipe.id')->where('mealplan_main.user_id', '=', $currentuserid)->get(['mealplan_main.Recipe_ID', 'recipe.recipe_name', 'mealplan_main.Day']);

return view('MealPlanDisplay.index', compact('data'));

是否可以在每次生成具有新膳食计划id的新膳食计划时拆分表?如果没有,除了表格之外,还有什么更好的方式来显示数据,因为我希望直观地显示彼此独立的用餐计划(因为用户可以有多个用餐计划)?

au9on6nz

au9on6nz1#

那么 $data 是一个集合,因此可以使用groupby()

$grouped = $data->groupBy('Meal_Plan_ID');

/*
[
    1 => [ /* data */ ], //Recepies for meal plan 1
    2 => [ /* data */ ], //Recepies for meal plan 2
]

* /

然后是html

@foreach ($grouped as $mealPlan)
        <!-- Init of new table or set pace -->
    @foreach ($mealPlan as $recepie)
        <!-- Recepie columns -->
    @endforeach
@endforeach

相关问题