【问题标题】:Can you split tables in a View during a foreach loop?您可以在 foreach 循环期间拆分视图中的表吗?
【发布时间】:2021-10-01 23:06:57
【问题描述】:

我正在处理一个膳食计划项目,为了视觉呈现,我想知道在 foreach 循环期间,每个具有唯一 MealPlan_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 表和食谱表。

MealPlanDisplayController中的索引方法:

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 生成新的膳食计划时拆分表?如果不是,那么除了表格之外,还有什么更好的方式来呈现数据,因为我想直观地呈现彼此分开的膳食计划(因为用户可以有多个膳食计划)?

【问题讨论】:

    标签: php html laravel eloquent


    【解决方案1】:

    在这种情况下$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
    

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2019-08-24
      • 1970-01-01
      相关资源
      最近更新 更多