Laravel How To Pass Multiple Parameters into Route Tutorial

Reading Time: 4 minutes
819 Views

Inside this article we will see the concept i.e Laravel How To Pass Multiple Parameters into Route Tutorial. Article contains the classified information about Defining routes with multiple parameters in laravel.

If you are looking to find a solution for Laravel Passing Multiple Parameters In Route to Controller then you are at the right place to learn and implement. Additionally we will see passing multiple parameters with Model binding concept.

Read More: How To Add Custom Attribute in Laravel Model Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Pass Multiple Parameters into Route

Suppose, we have a User controller. We will consider these sample data only to learn and understand.

Define Route

Code of 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");

Get All Parameters in Controller

Read More: How to Use DB Raw Query in Laravel Explained Tutorial

Code of 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);
    }
}    

How To Create Link To this Route

Code of a blade template file,

<a href="{{ route('users.posts.show', ['user_id' => 1, 'post_id' => 10]) }}">Show</a>

Example #2: Pass Multiple Parameters with Model Binding

Again, we have a User controller. We will consider these sample data only to learn and understand.

Define Route

Code of 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");    

Get All Parameters in Controller

Read More: Laravel How To Get Min Value of Column Tutorial

Code of 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!']);
    }
} 

How To Create Link To this Route

Code of a blade template file,

<a href="{{ route('users.posts.show', ['user_id' => 1, 'post_id' => 10]) }}">Show</a>    

We hope this article helped you to learn about Laravel How To Pass Multiple Parameters into Route Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.