How to pass multiple parameters in Laravel routes

How to pass multiple parameters in Laravel routes

Laravel is a powerful PHP framework used for developing web applications. Routing is an important aspect of any web application as it defines the way the application responds to a client request. Laravel makes it easy to define and handle routes with its simple and intuitive routing system. One common requirement when working with routes is the ability to pass multiple parameters to a single route. In this article, we'll explore how to pass multiple parameters in Laravel routes.

Routes/web.php:

<?php
use Illuminate\Support\Facades\Route; 
use App\Http\Controllers\CustomerController;
Route::get('users/{user_id}/posts/{post_id}',[CustomerController::class, 'show'])->name("users.posts.show");

 

App/Http/Controllers/CustomerController.php:

<?php
namespace App\Http\Controllers;  
use Illuminate\Http\Request;
    
class CustomerController 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);
    }
} 

 

Resources/views/home.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>how to pass multiple parameters in Laravel routes</title>
</head>
<body>
    <a href="{{ route('users.posts.show', ['user_id' => 1, 'post_id' => 10]) }}">Click Me</a>
</body>
</html>

I hope it is helpful to you. Thank you for reading this post.

Comments

No Comment posted Yet!

Leave a Reply

OK! You can skip this field.