Skip to content

URL::defaults() causes positional parameter misalignment with key-bound route parameters #57488

@alaminfirdows

Description

@alaminfirdows

Laravel Version

12.35.0

PHP Version

8.2.3

Database Driver & Version

9.3.0

Description

When using URL::defaults() together with route parameters that use model key binding (e.g., {team:slug} or {workspace:uuid}), Laravel can misalign positional parameters when generating URLs via route().

The issue occurs when a route has multiple parameters with key bindings and defaults are set using only the parameter name, without specifying the bound field. This causes Laravel to incorrectly assign values, sometimes resulting in empty query strings like ?team= or ?workspace=.

This happens whether the first parameter is required or optional.

Note:
There was an issue addressing this problem, but the initial example was misleading — it used an optional parameter, which caused some confusion about the actual scope of the bug.

* This works without any issues in v12.3.0

Steps To Reproduce

  1. Define route with model key binding:
Route::get('{team:slug}/projects/{project}', [ProjectController::class, 'show'])
    ->name('projects.show');
  1. Sets default route params from middleware:
URL::defaults(['team' => 'example-team']);
  1. Route Call:
// Case 1: Positional parameter
route('projects.show', 123);

// ❌ Current:
// Exceptions: Missing required parameter for [Route: projects.show] [URI: {team}/projects/{project}] [Missing parameter: project].

// ✅ Expected:
// http://laravel.test/example-team/projects/123


// Case 2: Named parameters
route('projects.show', ['project' => 123]);

// ❌ Current:
// http://laravel.test/example-team/projects/123?team=

// ✅ Expected:
// http://laravel.test/example-team/projects/123

Bug report repo link: https://github.com/alaminfirdows/laravel-route-bug-report

Summary:
The root cause is that when multiple route parameters use key bindings, setting defaults with only the parameter name causes Laravel to misalign positional parameters. Properly mapping defaults to both the parameter name and key binding field resolves this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions