ItSolutionStuff.com

Laravel Route Pass Multiple Parameters Example

By Hardik Savani • April 16, 2024
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, laravel 9, laravel 10 and laravel 11 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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get All Routes in Laravel?

Read Now →

Laravel Redirect to Route from Controller Example

Read Now →

How to Generate QR Code in Laravel?

Read Now →

Laravel 9 Resource Route and Controller Example

Read Now →

Laravel Vue Router Example From Scratch

Read Now →

How to Execute Artisan Command from Controller in Laravel?

Read Now →

Laravel Redirect to URL using redirect() Helper

Read Now →

Laravel Get Route Parameters in Middleware Example

Read Now →

How to Return JSON Response in Laravel?

Read Now →

How to Redirect to External URL in Laravel?

Read Now →

How to Get Current Route Name in Laravel?

Read Now →

How to Redirect Route with Querystring in Laravel?

Read Now →

How to Generate Route with Query String in Laravel?

Read Now →