Laravel Route Pass Multiple Parameters Example

By Hardik Savani November 9, 2022 Category : Laravel

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 Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

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

| 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 App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

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

{

dd($user_id, $post_id);

}

}

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 Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

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

| 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 App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use App\Models\Post;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

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:

I hope it can help you...

Tags :