Skip to content
Advertisement

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

I would like a button’s url to take it to a page with a ID in the URL.

Controller:

 public function printpreviewingredients($MealPlan_ID)
     {

//contains the value
$ingredientsid = $MealPlan_ID;

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

The button with 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>

Despite the value $ingredientsid not being blank, it still won’t place the value in the URL link. How can I pass a variable from the Controller method into a button url? I can get it working in a table with a @foreach loop, but presentation wise it doesn’t look right.

Advertisement

Answer

You must concatenate the variable, don’t use PHP inside of the single quote mark. When you use a single quote mark all the content inside of it is takes as string, even if you use PHP syntax.

Use:

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

Instead of:

href="{{ url('/printpreviewingredients/pdf/$ingredientsid') }}"
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement