Laravel Route Pass Multiple Parameters Example

Hey Developer,

In this short guide, we will show you laravel pass multiple parameters to route. you will learn how to pass multiple parameters in route in laravel. In this article, we will implement a how to pass two parameters in route in laravel. This post will give you a simple example of laravel multiple parameters in route. Here, Create a basic example of pass multiple parameters in route laravel.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

Laravel Routes provides a way to pass multiple parameters to the URL. I will give you the following two ways to pass multiple parameters to the route. so, let’s see the following examples:

1) Example 1: Laravel Route Pass Multiple Parameters

2) Example 2: Laravel Route Pass Multiple Parameters with Model Binding

let’s see both examples with output:

Example 1: Laravel Route Pass Multiple Parameters

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersUserController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/UserController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function show(Request $request, $user_id, $post_id)

{

dd($user_id, $post_id);

}

}

.medrectangle-4-multi-155{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

Use It:

<a href="{{ route('users.posts.show', ['user_id' => 1, 'post_id' => 10]) }}">Show</a>

Output:

1

10

Example 2: Laravel Route Pass Multiple Parameters with Model Binding

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersUserController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/UserController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsUser;

use AppModelsPost;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function show(Request $request, User $user, Post $post)

{

dd($user->toArray(), $post->toArray());

return response()->json(['success'=>'User Updated Successfully!']);

}

}

Use It:

<a href="{{ route('users.posts.show', ['user_id' => 1, 'post_id' => 10]) }}">Show</a>

Output:

Laravel Route Pass Multiple Parameters Example

I hope it can help you…

Leave a Comment