framework: Artisan generated resource controller vars don't match resource route params when model has multiple words.
- Laravel Version: 5.4.13
Description:
When using Artisan to generate a model (and resource controller) with multiple words, the route param doesn’t match the var names used in the controller.
The route param ends up snake case, while the var names are camel case.
Steps To Reproduce:
artisan make:model -mcr ProductionOrder
// routes/web.php
Route::resource('production-orders', 'ProductionOrderController');
// app/ProductionOrder.php (generated by Artisan)
public function show(ProductionOrder $productionOrder) {};
// wherever.php
dd(Route::getRoutes());
// #allRoutes: array:8 [
// "HEADapi/user" => Illuminate\Routing\Route {#250}
// "HEADproduction-orders" => Illuminate\Routing\Route {#240}
// "HEADproduction-orders/create" => Illuminate\Routing\Route {#239}
// "POSTproduction-orders" => Illuminate\Routing\Route {#238}
// "HEADproduction-orders/{production_order}" => Illuminate\Routing\Route {#237}
// "HEADproduction-orders/{production_order}/edit" => Illuminate\Routing\Route {#236}
// "PATCHproduction-orders/{production_order}" => Illuminate\Routing\Route {#235}
// "DELETEproduction-orders/{production_order}" => Illuminate\Routing\Route {#234}
// ]
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 15 (5 by maintainers)
In addition to snapey’s answer, you can also use dashes in the resource method and it’ll work just as usual.
Route::resource('my-route'), 'RouteController');Route will expect
{my_route}and controller will expect(MyRoute $myRoute)but it’ll work as usual. Seems like this is a new fix, since it didnt use to work.Laravel magically will inject a initiated ProductOrder model with the id given in the url at the placeholder: {production_order} .
https://laravel.com/docs/5.4/controllers#resource-controllers
@devcircus …because it doesn’t work as it should. See the first post.
@stefanoruth Yes, you can rename it, but the point of Artisan is to make things more convenient. It doesn’t just work when model names are multi-word in camel case.
As someone new to building Laravel apps, this is pretty confusing. It took me a long time to track down the problem, and even asking a more experienced Laravel dev was no help. Should be an easily-prevented problem. It’s obviously a simple mistake made when writing the code. No way whomever wrote it intentionally set something to send snake case but receive camel case.
Is there a problem with it not working or is it just the url formatting thats not fitting you? 😃