Lesson 7 of 50 – Laravel Route Parameters
14%

Laravel Route Parameters

Route parameters allow you to capture values from a URL. You can use these values to display student details, course information, product records, and other dynamic content.

1. What Are Route Parameters?

A route parameter is a placeholder inside a URL. Laravel extracts the value from the URL and passes it to the route callback or controller method.

Example:

Route::get('/student/{id}', function ($id) {
    return 'Student ID: ' . $id;
});

Open this URL:

http://127.0.0.1:8000/student/101

Output:

Student ID: 101

Here, {id} is the route parameter, and $id receives its value.

2. Route with a Student Name

You can pass a student's name through the URL.

Route::get('/student-name/{name}', function ($name) {
    return 'Welcome, ' . $name;
});

Example URL:

/student-name/Rahul

Output:

Welcome, Rahul

You can use this technique to create dynamic pages for students, teachers, products, or courses.

For real applications, validate input and escape user-provided values appropriately when displaying them in HTML.

3. Multiple Route Parameters

A route can contain two or more parameters.

Route::get('/student/{id}/course/{course}', function ($id, $course) {
    return 'Student ID: ' . $id . ', Course: ' . $course;
});

Example URL:

/student/101/course/PHP

Output:

Student ID: 101, Course: PHP

Laravel passes the route parameter values to the callback in the order defined by the route.

4. Optional Route Parameters

An optional parameter allows a URL to work even when the parameter is not provided.

Route::get('/greeting/{name?}', function ($name = 'Student') {
    return 'Hello, ' . $name;
});

URL 1:

/greeting

Output:

Hello, Student

URL 2:

/greeting/Amit

Output:

Hello, Amit

Remember to use ? in the route parameter and provide a default value in the callback or controller method.

5. Restrict Parameters to Numbers

Sometimes a route should accept only numeric IDs. Laravel provides whereNumber() for this.

Route::get('/student/{id}', function ($id) {
    return 'Student ID: ' . $id;
})->whereNumber('id');

Matching URL:

/student/101

Non-matching URL:

/student/abc

The second URL does not match this route because the parameter is not numeric. If no other route matches, Laravel returns a 404 response.

Use route constraints to prevent invalid URL values from matching a route.

6. Restrict Parameters with a Pattern

You can use where() to define a regular expression for a route parameter.

Allow letters only:

Route::get('/category/{name}', function ($name) {
    return 'Category: ' . $name;
})->where('name', '[A-Za-z]+');

This matches a category such as /category/Books.

Allow digits only:

Route::get('/order/{id}', function ($id) {
    return 'Order ID: ' . $id;
})->where('id', '[0-9]+');

This route accepts a numeric order ID such as /order/500.

The route constraint controls which URL values match; it does not replace application-level validation or authorization.

7. Route Parameters with Views

You can pass a route parameter to a Blade view.

In routes/web.php:

Route::get('/course/{name}', function ($name) {
    return view('course', ['courseName' => $name]);
});

Create the file resources/views/course.blade.php.

Inside course.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Course Details</title>
</head>
<body>
    <h1>Course Details</h1>

    <p>Course Name: {{ $courseName }}</p>
</body>
</html>

Example URL:

/course/Laravel

The page displays the course name using Blade's escaped output syntax, {{ }}.

8. Route Parameters with Controllers

For larger applications, place route-handling logic inside controllers rather than writing everything in a route closure.

Create a controller:

php artisan make:controller StudentController

Open app/Http/Controllers/StudentController.php and add a method inside the controller class:

public function show($id)
{
    return 'Student ID: ' . $id;
}

Register the route in routes/web.php:

use App\Http\Controllers\StudentController;

Route::get('/students/{id}', [StudentController::class, 'show'])
    ->whereNumber('id');

When a visitor opens /students/101, Laravel calls the controller's show() method and passes the ID.

9. Practical Assignment

Practise route parameters by creating the following routes in routes/web.php.

  1. Create /student/{id} to display a student ID.
  2. Create /teacher/{name} to display a teacher's name.
  3. Create /product/{id}/name/{name} to display a product ID and name.
  4. Create an optional /welcome/{name?} route with a default name.
  5. Restrict a student ID parameter to numeric values.
  6. Use where() to allow letters only in a category name.
  7. Pass a course name from a route to a Blade view.
  8. Create a controller method that receives a student ID.
  9. Run php artisan route:list and inspect your routes.

Test both valid and invalid URLs. Make sure each route has a unique path or an intentional constraint so that routes do not conflict.


🧠 Quick Quiz

1. What does the parameter {id} represent in a route?




2. Which symbol makes a route parameter optional?




3. Which method restricts a route parameter to numbers?