Closed
Description
Laravel Version
12.18.0
PHP Version
8.3
Database Driver & Version
No response
Description
If we declare a nullable param in a controller action, and the route targeting that controller action does not have that param, the binding is not working as expected.
Instead of passing null
, it pass an empty Model, or may fail.
Steps To Reproduce
Declare 2 routes targeting the same controller action:
Route::controller(UsersController::class)->group(function () {
Route::get('users/{user}/details', 'show')->withTrashed();
Route::get('companies/{company}/users/{user}/details', 'show')->withTrashed();
});
In UsersController
controller:
If you try to access to URL: /users/1/details
// Same order as URL: KO
public function show(?Company $company, User $user)
{
var_dump($company);
// Expected: null
// Actual result: an empty company model object
}
// Reverse order: OK
public function show(User $user, ?Company $company = null)
{
var_dump($company);
// Expected: null
// Actual result: null
}
If you try to access to URL: /companies/1/users/1/details
// Same order as URL: OK
public function show(?Company $company, User $user)
{
// Works as expected
}
// Reverse order: KO
public function show(User $user, ?Company $company = null)
{
// It tries to bind a Company to $user
// From the documentation, the implicit binding is supposed to match the param names, so the order should not matter:
// https://laravel.com/docs/12.x/routing#implicit-binding
// type-hinted variable names match a route segment name
}
Metadata
Metadata
Assignees
Labels
No labels