How to Pass Route Multiple Parameters in Laravel?

Jan 12, 2023

.

Admin

How to Pass Route Multiple Parameters in Laravel?

Hi Guys,

We'll teach you how to use laravel to pass several parameters to a route in this brief guide. you'll discover how to use numerous arguments in laravel's route. we will demonstrate how to pass two arguments in a route in laravel in this article. you'll get a straightforward example of laravel multiple parameters in route in this post. create a simple example of passing multiple arguments in a laravel route here.

Multiple arguments can be passed to the URL using Laravel Routes. I'll tell you the next two approaches to instruct the route to accept multiple parameters. Let's look at the following instances:

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>

I hope it can help you...

#Laravel