Acquiring URL Parameters in Laravel Controller: A Guide

Feedback


Question:

my view blade …..

  tableHtml += " add to favotite";

Upon selecting “add to fav,” the URL displays the following.

The link provided directs to the addition of a “Chilli Green Salad” on the Lily website, which can be accessed through the local host.

web.php

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

What is the method to obtain the name of
url pass
in the controller?

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


Solution 1:

It is necessary to include
parameter to the route
in order to achieve the desired outcome. The final result should appear as follows.

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

And the

add

method:

public function add(Request $request, $slug)

The variable named

$slug

will hold the value of

Chilli Green Salad

.

Refer to the Laravel 5.5 documentation on routing to learn about parameters that are mandatory.


Solution 2:

You can do it like,


Route

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


Controller

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

I hope you will understand.


Solution 3:

In your

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 will help!


Solution 4:

Alter your url,add a get variabile

tableHtml += " add to favotite";

in your controller you use

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

Frequently Asked Questions

Posted in Php