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, 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get All Routes in Laravel?
- Laravel Redirect to Route from Controller Example
- How to Generate QR Code in Laravel?
- Laravel 9 Resource Route and Controller Example
- Laravel Vue Router Example From Scratch
- How to Execute Artisan Command from Controller in Laravel?
- Laravel Redirect to URL using redirect() Helper
- Laravel Get Route Parameters in Middleware Example
- How to Return JSON Response in Laravel?
- How to Redirect to External URL in Laravel?
- How to Get Current Route Name in Laravel?
- How to Redirect Route with Querystring in Laravel?
- How to Generate Route with Query String in Laravel?