I am developing an API through Laravel using Resource Controllers which are linked through the routes.php
file. See the example below:
// API routes
Route::group(array('prefix' => 'v1'), function() {
Route::resource('users', 'UserController');
Route::resource('users.profile', 'UserProfileController');
Route::resource('users.tasks', 'UserTaskController');
});
Generally, Laravel does a good job at handling these routes. But there is one exception that I have where I struggle with. My database model is designed to have a one-to-one relationship between the users
table and the user_profiles
table by looking at the foreign key user_id
in the user_profiles
table. This means that it is acting on a identifying relationship, so it doesn't have an own primary key, it actually borrows the key of the users
table.
Now, I want to update the UserProfile
model through the UserProfileController
that gets called when the following (example) URL is entered: http://api.projectX.dev:8000/v1/users/{id of the user}/profile
. But Laravel forces me to have a URL like this: http://api.projectX.dev:8000/v1/users/{id of the user}/profile/{some other useless ID}
.
Is there any way to magically remove that last ID from the resources route and just take advantage of the first ID?
I hope that somebody can help me with this, because finding the answer to this question is actually pretty hard to find right now.