laravel resource controllers / routes and models

Question

So if you do something like

$ artisan make:model TurboClown
$ artisan make:controller TurboClownController -r --model=TurboClown

So at this point it's possible to add something like:

Route::resource('clowns','TurboClownController');

To your routes/web.php. Now I have routes like clowns/{clown} when I routes:list with artisan.

However, my show function is like:

    public function show(TurboClown $turboClown)

Which when you return $turboClown will just give [] as a response when you request "/clowns/3/" for example. It took me a while to figure out that if I change $turboClown parameter to $clown, I get a JSON TurboClown as a response.

So in a sense, I solved my problem. But I'm curious about a couple points:

  1. To me, https://laravel.com/docs/5.4/controllers#restful-naming-resource-route-parameters reads as if I could add ['parameters' => ['clown' => 'turboClown'] and then show() would work using "turboClown", but it does not.

  2. According to https://laravel.com/docs/5.4/routing#route-parameters : " Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter." But it looks like the name of the parameter does matter in this case?

So I am looking for some kind of clarification on those two points, what am I missing?


Show source
| laravel   | php   2017-08-08 23:08 1 Answers

Answers to laravel resource controllers / routes and models ( 1 )

  1. 2017-08-08 23:08
    1. You are doing it wrong. The documentation says:

    The parameters array should be an associative array of resource names and parameter names

    So in your case the resource name is "clowns" (and not "clown") and you want for this resource name the parameter to be "turboClown:

    Route::resource('clowns', 'TurboClownController', ['parameters' => [
        'clowns' => 'turboClown'
    ]]);
    
    1. I agree that this point can be confusing. Maybe you already know that but what you are doing here is called implicit route model binding. And for it to be "implicit", there is one rule overruling the one you mentioned:

    Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

    Also for this pretty cool thing to work the parameter has to match an identifier for the corresponding model (in your case TurboClown).

    So now combining those two points you should be able to do this:

    In your routes file:

    Route::resource('clowns', 'TurboClownController', ['parameters' => [
        'clowns' => 'turboClown'
    ]]);
    

    In TurboClownController:

    public function show(TurboClown $turboClown)
    {
        return $turboClown;
    }
    

    Now let's say you want to retrieve the turbo clown with the identifier 5. You can cal the route http://example.dev/clowns/5.

    Hope it helped.

Leave a reply to - laravel resource controllers / routes and models

◀ Go back