带有在路由中传递变量的 href 的视图上的按钮?

Button on a view with a href that has a variable passed in the route?

我想要一个按钮的 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 link。如何将变量从 Controller 方法传递到按钮 url?我可以使用 @foreach 循环在 table 中使用它,但在演示方面它看起来不正确。

必须连接变量,不要在单引号内使用PHP。 当你使用单引号时,它里面的所有内容都被当作字符串,即使你使用 PHP 语法。

使用:

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

而不是:

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