phplaravel

how to get url parameter in controller in laravel?


my view blade .....

  tableHtml += "<td><a href= '{{url('/add')}}/" + data.hits[i].recipe.label + "'> add to favotite</a></td>";

when i click add to fav....i get this in url

http://localhost/lily/public/add/Chilli%20Green%20Salad

web.php

Route::get('/add', 'HomeController@add');

how can i get the url pass name in controller .....

public function add(Request $request)
{
 
$request->get("") ////////////how can i get the string i passed on url 

}

Solution

  • You need to add the parameter to the route. So, it should look like this:

    Route::get('add/{slug}', 'HomeController@add');
    

    And the add method:

    public function add(Request $request, $slug)
    

    Then value of the $slug variable will be Chilli Green Salad

    https://laravel.com/docs/5.5/routing#required-parameters