Laravel: Dynamic routes

We’ve seen how to create static routes with Laravel.

Sometimes you want your routes to be dynamic.

This will be especially useful with databases, but let’s do an example without first.

In routes/web.php add an entry like this:

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

and in resources/views/test.blade.php write this code:

@if (isset($name))
  Hello {{$name}}
@else
  Test
@endif

Now if you navigate with your browser to the /test/flavio route, “flavio” is the $name parameter in the route, which is passed to the view, so you can print it in the Blade template:

Change the route parameter, the name in the HTML changes:

Lessons in this unit:

0: Introduction
1: Getting started
2: Blade
3: ▶︎ Dynamic routes
4: Adding a database
5: How to use migrations to create and modify the database schema
6: Using forms to accept user input and store it into the database
7: Adding a better layout
8: Adding the form at the bottom of the list
9: Allow users to delete dogs from the list
10: Adding authentication using Laravel Breeze
11: Only authenticated users can add items to the database
12: Push the app code to GitHub
13: Deployment
14: Dynamic routes advanced
15: Non-web routes
16: Creating commands

Join my AI Workshop!

The Web Development BOOTCAMP cohort starts in February 2026