Category : #laravel

Tags : #laravel, #laravel routing

In this example, I will show you how to pass parameters and multiple parameters in the Laravel route. If you don't know how to pass multiple parameters and single parameters then this example is for you.

Assume, you may need to capture a user's ID from the URL. You may do like:

<?php

use App\Http\Controllers\TestController;
use Illuminate\Support\Facades\Route;

Route::get('/user/{id}', [TestController::class,'index']);

 

Now we will get this user id inside our controller method like:

App\Http\Controllers\TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index($id)
    {
        return $id;
    }
}

 

You may define as many route parameters as required by your route:

Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

 

Read also: Laravel 9 Route View Example

 

Hope it can help you.