【问题标题】:Is it possible to return a view (using blade) with two variables in laravel?是否可以在 laravel 中返​​回带有两个变量的视图(使用刀片)?
【发布时间】:2022-01-03 14:46:19
【问题描述】:

我必须在其中一个页面上创建一个表单,该表单将根据菜肴类型或菜肴类型等搜索餐点及其食谱...... 这是我返回视图的函数,但我在下面以粗体显示的行中有一个错误:

public static function SearchPage()
{
    $DishType = DishType::getAllDishType();
    $CuisineType = CuisineType::getAllCuisineType()

    return view("vueRecipeSearch", [
        'DishType' => DishType::getAllDishType(),
        CuisineType::getAllCuisineType()
    ]);
}

这是视图的一部分,我想在其中使用变量:

<label for="DishType">Dish type :</label>
  <select id="DishType" name ="DishType">
      @foreach($DishType as $Dish)
        <option value="Dessert">{{$Dish->dish}}</option>
      @endforeach
  </select>

我想对美食类型做同样的事情,但不能因为我不知道如何返回多个变量..

【问题讨论】:

  • 应该是传递给view的关联数组...键是视图中变量的名称...您没有为第二个值分配键"以这种方式传递信息时,数据应该是具有键/值对的数组。”

标签: php html laravel laravel-blade


【解决方案1】:
return view("vueRecipeSearch", [
        'DishType' => DishType::getAllDishType(),
        'CuisineType' => CuisineType::getAllCuisineType()
    ]);

然后您可以使用数组键访问刀片文件:DishType 和 CuisineType。

【讨论】:

    【解决方案2】:

    显然可以将两个变量从控制器发送到刀片。

    $dishType'   = DishType::getAllDishType();
    $cuisineType = CuisineType::getAllCuisineType();
    
    return \view("vueRecipeSearch", compact('dishType', 'cuisineType'));
    

    在刀片文件上,您必须通过$dishType 和$cuisineType 访问这两个变量。

    【讨论】:

      【解决方案3】:

      return view ('vueRecipeSearch',compact('dishType', 'cuisineType')); 无需添加正斜杠或反斜杠。简单

      【讨论】:

        猜你喜欢
        • 2015-03-13
        • 2013-06-09
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 2014-03-15
        相关资源
        最近更新 更多