0
votes

I'm trying to figure out the correct syntax to use here in both the href link in my blade and the {id} passing in the route, but the documentation for laravel (5.8) has me a bit confused.

My controller function should be good, but I'm wondering how I would appropriately pass an ID (currently echoed in the blade) properly through the button href link, and then handle that ID properly in the route so that I get not only the page at the url id\progress but also get the $id in the controller function.

Am I on the right track?

blade.php

<?php echo($id) ?>
<button><a href=""></a></button>

web.php (routes)

Route::get('/{id}/progress', 'Track\TrackProgressController@index')
->name('track.progress');

TrackProgressController

public function index($id)
{

}
4
Yes you are on the right track just add url in your achor tag like: <a href="{{ route('track.progress', $id) }}"></a> - Vikramjit SIngh
Did you check the Laravel documentation? laravel.com/docs/5.8/routing#named-routes - Gopal Kildoliya

4 Answers

2
votes

You can use Laravel variable in herf tag like:

{{ route('track.progress', ['id' => $id]) }}

However, you are passing $id correctly in your routes and function

Route

 Route::get('/{id}/progress', 'Track\TrackProgressController@index')
->name('track.progress');

Funcation

public function index($id)
{

}
2
votes

You can use the code like this in blade:

<button><a href="/{{$id}}/progress"></a></button>

which will pass the $id as a param to the route. Your route will be like this

Route::get('/{id}/progress', 'Track\TrackProgressController@index')->name('track.progress');

You can access the id in controller like this:

public function index($id = null)
{
    dd($id);
}
1
votes

You're on the correct path, but please change your route definition to match the correct syntax from official documentation:

Route::get('/progress/{id}', 'Track\TrackProgressController@index')
->name('track.progress');

Then, you can add id as your route parameter in your view:

<button><a href="/progress/{{$id}}"></a></button>

Or another way, since you have a route name:

<button><a href="{{ route('track.progress',['id' => $id])}}"></a></button>

Then, all you have to do is to retrieve that id inside your controller, like this:

public function index($id)
{
  echo 'Retrieved id = '.$id;
}
0
votes

To pass single (parameter) id from blade to controller you can simple use href if you are using get method..

Blade:

<a href="{{ route('track.progress', $id)}}"></a>

Web.php:

Route::get('/{id}/progress', 'Track\TrackProgressController@index')
->name('track.progress');

Controller:

public function index($id)
{
        // do something
}

and to pass multiple parameters to controller then you can use array[] in route method:

<a href="{{ route('multi.params', ['param1'  => 'param1', 'param2' => 'param2'])}}"></a>

Route for Multiple Params:

Route::get('/{param1}/{param2}', 'MyController@MyMethod')->name('multi.params');

Controller:

public function MyMethod($param1, $param2)
{
        // do something
}

Thanks