How to Pass Route Multiple Parameters in Laravel?
Teams applying “How to Pass Route Multiple Parameters in Laravel?” in a production project can use the linked implementation guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
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>
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial