首页 > 解决方案 > 带有href的视图上的按钮在路由中传递了一个变量?

问题描述

我想要一个按钮的 url 把它带到一个在 URL 中有 ID 的页面。

控制器:

 public function printpreviewingredients($MealPlan_ID)
     {

//contains the value
$ingredientsid = $MealPlan_ID;

return view('MealPlanDisplay.printpreviewingredientslist', compact('ingredientsid'));

带有 URL 的按钮:

  <div class="d-flex justify-content-end mb-4">
              <a class="btn btn-primary" href="{{ url('/printpreviewingredients/pdf/$ingredientsid') }}">Export to PDF</a>
          </div>

尽管 $ingredientsid 的值不是空白,但它仍然不会将该值放在 URL 链接中。如何将 Controller 方法中的变量传递到按钮 url?我可以让它在带有@foreach 循环的表中工作,但从演示角度来看,它看起来不正确。

标签: phplaravelbutton

解决方案


您必须连接变量,不要在单引号内使用 PHP。当您使用单引号时,其中的所有内容都被视为字符串,即使您使用 PHP 语法也是如此。

利用:

href="{{ url('/printpreviewingredients/pdf/'.$ingredientsid) }}"

代替:

href="{{ url('/printpreviewingredients/pdf/$ingredientsid') }}"

推荐阅读