How to get url parameter in controller in laravel?

my view of the 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 '); 

//// controller ...... how can I get the name of the url in the controller .....

 public function add(Request $request) { $request->get("") ////////////how can i get the string i passed on url } 
+5
source share
4 answers

You need to add a 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 the value of the $slug variable will be Chilli Green Salad

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

+4
source

Change your url, add get variabile

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

in your controller you use

 public function add(Request $request) { echo $request->slug; } 
+1
source

In router.php :

 Route::get('/add/{recipe}', ' HomeController@add '); // if recipe is required Route::get('/add/{recipe?}', ' HomeController@add '); // if recipe is optional 

In your controller:

 public function add(Request $request, $recipe) { // play with $recipe } 

Hope this helps!

0
source

You can do it,

Route

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

controller

 public function add(Request $request){ // Access data variable like $request->data } 

I hope you understand.

0
source

Source: https://habr.com/ru/post/1274142/


All Articles