All Routing lessons

Route parameters

5 min read

Use {name} segments to capture values from the URL.

Route::get('/posts/{post}', function (string $post) {
    return "Post $post";
});

Optional parameters

Add ? and a default:

Route::get('/users/{name?}', fn ($name = 'guest') => "Hello, $name");

Constraints

Use ->where() or named constraints (->whereNumber, ->whereAlpha):

Route::get('/posts/{id}', fn ($id) => …)->whereNumber('id');

Route-model binding (the killer feature)

If you type-hint the parameter with a model class and the segment matches the model's route key (defaults to id), Laravel resolves it for you and 404s if missing:

Route::get('/posts/{post}', function (App\Models\Post $post) {
    return $post; // already an Eloquent instance
});

Override the lookup key per-model in getRouteKeyName() (e.g. 'slug').