How to define a route with multiple parameters in Laravel
In this tutorial, we will learn How to define routes with multiple parameters in Laravel. we will implement a how-to pass two parameters in route in Laravel. In this post, we will learn about the Laravel route to the controller with multiple parameters with an example. I will show you Laravel passes multiple parameters to the route. This post will give you a simple example of How to define routes with multiple parameters in Laravel.
You can use this example with Laravel 6, Laravel 7, Laravel 8, and Laravel 9 versions.
I will give you two ways to pass multiple parameters to the route.
- Laravel Route Pass Multiple Parameters
- Laravel Route Pass Multiple Parameters with Model Binding
When we use the required parameter then we will pass the value in route else it gets errors. So, these parameter key names are defined in the routes web.php file, and then we get their value in the controller method by the corresponding key name. let’s see the example:
Laravel Route Pass Multiple Parameters
Route file
<?php
#routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
/*
|--------------------------------------------------------------------------
| 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('booking/{booking_id}/event/{event_id}',[App\Http\Controllers\HomeController::class, 'index'])->name("booking");
Controller file
<?php
#app/Http/Controllers/HomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($booking_id, $event_id)
{
dd($booking_id, $event_id);
}
}
Blade file
#index.blade.php
<a href="{{ route('booking', ['booking_id' => 3, 'event_id' => 7]) }}">Laravel Route Pass Multiple Parameters</a>
Laravel Route Pass Multiple Parameters with Model Binding
Route file
<?php
#routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
/*
|--------------------------------------------------------------------------
| 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('booking/{Booking}/event/{Event}',[App\Http\Controllers\HomeController::class, 'index'])->name("booking");
Controller file
<?php
#app/Http/Controllers/HomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\Event;
class HomeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Bookings $booking, Event $event)
{
dd($booking->toArray(), $event->toArray());
}
}
Blade file
#index.blade.php
<a href="{{ route('booking', ['Booking' => 3, 'Event' => 2]) }}">Laravel Route Pass Multiple Parameters with Model Binding</a>