Hi there!
In a fresh Laravel 5.3 installation, I'm having a problem when using route model binding in a specific resource. To all my other resources it works fine, but for this specific one, look what is happening:
This is the route declaration using a resourcefull controller:
Route::resource('ordem.conta', 'OrdemContaController', ['except' => 'show']);
So, in the controller file I'll expect the following bindings in the @edit action:
public function edit(Ordem $ordem, Conta $conta)
{
...
}
That shall be fired when I call this URL:
localhost/ordem/38/conta/38/edit
But, something weird happened. The Ordem model was found as expected, but Conta model was not. So I ran php artisan route:list to look for any issues in these routes, and I found this:
GET|HEAD | ordem/{ordem}/conta/{contum}/edit | ordem.conta.edit | App\Http\Controllers\OrdemContaController@edit
As you can see, conta became contum by some reason. And this was responsible for not finding my Conta model through model binding.
The same issue exists for PUT/PATCH and DELETE, which requires model binding for the Conta object.
Is this a bug, or there is a way to disable this "contum" word?
Best regards,
Rafael Pacheco.
Laravel assumes the URI is in english, so it tries to follow the english rules of singular/plural forms using Doctrine Inflector, if you provide a name users Laravel will try to look for a model User.
As for Conta, just like data, it thinks the singular form would be contum.
I suggest if you're not using english in your resource names to do the following:
Route::resource('ordem.conta', 'OrdemContaController', ['except' => 'show', 'parameters' => [
'ordem' => 'ordem',
'conta' => 'conta',
]]);
Is this a new behavior in 5,3? In 5.2 and before it works just fine.
cc @themsaid
I think that's new in 5.3 yeah.
Thanks!
I will set the parameters as you said.
Best regards,
Rafael Pacheco.
Perfect :)
Hi @themsaid, it's me again.
I found that this singular transformation do exists in Laravel 5.2, but it was not changing my routes in 5.2 Only in 5.3 it started to changing them to singular.
So I went to the ResourceRegistrar class, and found the following default configuration:
In Laravel 5.2
protected static $singularParameters = false;
In Laravel 5.3
protected static $singularParameters = true;
As you can see, 5.3 defaults to singularization, and 5.2 defaults to respect the resource name.
This is a breaking change, and shouldn't it be documented in the migration guide? Or even change 5.3 to false just like 5.2 did?
Best regards,
Rafael Pacheco.
It's documented under "Resource Parameters Are Singular By Default" in the upgrade guide, you can also override this behaviour using:
Route::singularResourceParameters(false);
My fault, the documentation explains very clear the new behavior.
Thank you @themsaid for your help!