Jump to content

Laravel 5.4 rest controller with composite key table


Recommended Posts

I've managed to sort of a simple REST test, which uses a standard model with a single field for the key.

I've also managed to get Eloquent to deal with composite key models.

Now I want to see how I can put them both together - in the simplest way possible! So i would like it to fit into the standard

Route::resource('/Bin', 'RestBinController');

This only seems to expect a single parameter and so if I try /api/Bin/1/2 it doesn't recognise the path.

I could split it out into the get/post... methods (and it may come down to this) but I wanted to check if I was missing something before I did this.

Link to post
Share on other sites

Looks like using dot notation will get you part of the way there.

Route::resource('bin/foo.bar', 'RestBinController');
❯ ./artisan route:list
+--------+-----------+------------------------------+-----------------+------------------------------------------------+--------------+
| Domain | Method    | URI                          | Name            | Action                                         | Middleware   |
+--------+-----------+------------------------------+-----------------+------------------------------------------------+--------------+
|        | GET|HEAD  | /                            |                 | Closure                                        | web          |
|        | GET|HEAD  | api/user                     |                 | Closure                                        | api,auth:api |
|        | GET|HEAD  | bin/foo/{foo}/bar            | foo.bar.index   | App\Http\Controllers\RestBinController@index   | web          |
|        | POST      | bin/foo/{foo}/bar            | foo.bar.store   | App\Http\Controllers\RestBinController@store   | web          |
|        | GET|HEAD  | bin/foo/{foo}/bar/create     | foo.bar.create  | App\Http\Controllers\RestBinController@create  | web          |
|        | GET|HEAD  | bin/foo/{foo}/bar/{bar}      | foo.bar.show    | App\Http\Controllers\RestBinController@show    | web          |
|        | PUT|PATCH | bin/foo/{foo}/bar/{bar}      | foo.bar.update  | App\Http\Controllers\RestBinController@update  | web          |
|        | DELETE    | bin/foo/{foo}/bar/{bar}      | foo.bar.destroy | App\Http\Controllers\RestBinController@destroy | web          |
|        | GET|HEAD  | bin/foo/{foo}/bar/{bar}/edit | foo.bar.edit    | App\Http\Controllers\RestBinController@edit    | web          |
+--------+-----------+------------------------------+-----------------+------------------------------------------------+--------------+

Not exactly the /api/bin/1/2 you were describing but close, and perhaps a little more descriptive.

Link to post
Share on other sites

Oh wait. Composite keys. /api/bin/1/2 points to a single object. Disregard my earlier suggestion, then. I suspect resourceful controllers will fall short and you will need to be more declarative in your routing.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.