Jump to content


Photo

Laravel 5.4 rest controller with composite key table


  • Please log in to reply
2 replies to this topic

#1 NigelRel3

NigelRel3
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 20 April 2017 - 08:51 AM

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.



#2 dkub

dkub
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 April 2017 - 10:49 AM

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.



#3 dkub

dkub
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 April 2017 - 11:08 AM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users