Multiple parameter pass through Route & URL in Laravel-WebJourney

Multiple parameter pass through Route & URL in Laravel-WebJourney

Understanding how to use multiple parameters in Laravel is an essential part of building robust web applications with this popular PHP framework. In this article, we will explore how to pass multiple parameters in a route and URL in Laravel.

 

Defining a Route with Multiple Parameters:

To define a route with multiple parameters, you can use the bellow syntax in your routes/web.php file:

Route::get('/users/{id}/{name}', ['UserController::class,'show'])->name('users.show');

 

In this example, we have defined a route that accepts two parameters: id and name. These parameters will be passed to the show method of the UserController class when this route is accessed. You can define as many parameters as you need by adding additional placeholders in the route definition.

 

Generating a URL with Multiple Parameters:

To generate a URL that includes multiple parameters, you can use the route helper function in your views or controllers.

For example, to generate a URL for the route we defined above, you can use the following code:

$url = route('users.show', ['id' => 1, 'name' => 'Web Journey']);
<a href="{{route('users.show', ['id' => 1, 'name' => 'Web Journey']}}">Show User</a>

In this example, we have specified the values for the id and name parameters in an array passed as the second argument to the route function. The first argument is the name of the route, which in this case is users.show.

 

Using Multiple Parameters in Controller Actions:

To access the parameters passed in the URL in your controller action, you can add parameters to the method signature that match the placeholders defined in the route. For example, to access the id and name parameters in the UserController@show method, you can use the following code:

public function show($id, $name)
{
    // Do something with $id and $name
}

 

In this example, we have added $id and $name parameters to the method signature. These parameters will automatically be populated with the values passed in the URL when this method is called.

 

Conclusion:

In this article, we have explored how to pass multiple parameters in a route and URL in Laravel. By defining routes with multiple placeholders, generating URLs with the route helper function, and accessing parameters in controller actions, you can create powerful and flexible routes that meet the needs of your application.

 

 

 

 

If you like what you are reading, please think about buying us a coffee as a token of appreciation.

Buy Me A Coffee

We appreciate your support and are committed to providing you useful and informative content.

We are thankful for your ongoing support.

 

 

 

 

You May Read Bellow Articles:

 

 

Laravel 9 live search data in a table using ajax

 

How to send SMS in laravel 9 using Twilio SMS API-Webjourney

 

Laravel 9 pdf invoice generate and download with barryvdh dompdf

 

How to create multi language website by laravel 9

 

Laravel 10 multiple form validation on the same page-WebJourney

 

Tags

  • Share This: