Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get url parameter in controller in laravel?

Tags:

php

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 

}
like image 691
Nahid Hasan Avatar asked Dec 18 '17 07:12

Nahid Hasan


People also ask

How can I get route name in laravel?

You may use the current, currentRouteName, and currentRouteAction methods on the Route facade to access information about the route handling the incoming request: $route = Route::current(); $name = Route::currentRouteName(); $action = Route::currentRouteAction();

What is Uri in laravel?

Routing in Laravel allows you to route all your application requests to their appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing.


1 Answers

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

like image 147
Alexey Mezenin Avatar answered Sep 22 '22 10:09

Alexey Mezenin