laravel雄辩:连接表并检索特定值?

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

我对拉拉维尔是个新手,我正在做一个膳食计划项目。我想拥有的功能之一是能够打印出一张包含特定膳食计划中所有成分的表格。

膳食计划表

身份证件
mealplan_id
配方id

配方表

身份证件
配方名称

配料表

身份证件
配方id
成分名称
数量
一餐计划有很多食谱。一个食谱有很多成分。
在显示用餐计划的视图中,我希望有一个按钮,如 Preview Ingredients Print list ,然后调出一个视图,显示与特定膳食计划中所有食谱相关的所有配料。
我已经试着向你提出申请了 MealPlan_ID (每个单独用餐计划的唯一标识符,具有多个食谱id),然后将表连接在一起。因为膳食计划表和配料表都有 Recipe_ID ,是否可以在联接中完全忽略配方表?

public function printpreviewingredients($MealPlan_ID)
{
    //take the Meal Plan ID, to then collect the correct Recipe ID's and all of the following ingredients.
    $ingredients = MealPlanDisplay::find($MealPlan_ID)
        ->join('recipeingredientsmain', 'recipeingredientsmain.recipe_id', '=', 'mealplan_main.Recipe_ID')
        ->get(['recipeingredientsmain.id', 'recipeingredientsmain.ingredientname', 'recipeingredientsmain.amount']);

    return view('MealPlanDisplay.printpreviewingredientslist', compact('ingredients'));
}

这个 web.php 路线为:

Route::get('printpreviewingredients/{$MealPlan_ID}', [MealPlanDisplayController::class, 'printpreviewingredients']);

景色 printpreviwingredientslist.blade.php 然后包含一个包含信息的表 ingredients :

<thead class="bg-gray-50">
    <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 ID
        </th>
        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
            Ingredient Name
        </th>
        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
            Amount
        </th>
    </tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
    @foreach ($ingredients as $var)
        <tr>
            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                {{$var->id}}
            </td>
            <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->ingredientname}}
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                {{$var->amount}}
            </td>
        </tr>
    @endforeach
</tbody>

不幸的是,这返回一个404错误,带有 page not found . 我真的不知道如何使用三表关系来实现这种连接和信息检索。有什么建议吗?

9vw9lbht

9vw9lbht1#

这是因为您在返回视图中定义了不正确的文件。请检查您的文件名拼写。
若要使用laravel eloquent连接表格,请遵循下面给出的答案
如何用laravel雄辩模型连接三个表

相关问题