laravel
laravel copied to clipboard
[Q] Shouldn't be 'index' the route name for the listing action of a HasMany relationship?
I have in my route:
# routes/api.php
$server
->resource('users', JsonApiController::class)
->relationships(function (Relationships $relations) {
$relations->hasMany('user-organizations');
});
# UserSchema.php
public function fields(): array
{
return [
// ...
Relations\HasMany::make('user-organizations')->type('user-organizations'),
];
}
But the resulting route name is api.v1.users.user-organizations instead of api.v1.users.user-organizations.index:
GET|HEAD api/v1/users/{user}/relationships/user-organizations ... api.v1.users.user-organizations.show
PATCH api/v1/users/{user}/relationships/user-organizations ... api.v1.users.user-organizations.update
POST api/v1/users/{user}/relationships/user-organizations ... api.v1.users.user-organizations.attach
DELETE api/v1/users/{user}/relationships/user-organizations ... api.v1.users.user-organizations.detach
GET|HEAD api/v1/users/{user}/user-organizations ................. api.v1.users.user-organizations
Am I missing something, or it's OK?
How can I change it to have 'index' by default in all routes of this kind, instead of the "naked" route action?
It's been that way for a long time now, no one has ever raised it! Not sure it causes any problems?
I noticed because I get an error from my route() function in my front. It builds the endpoint URL by using the route name instead of the route path directly.
I was automating/normalizing my http client in my Vue application and got confused when I got this inconsistency. 😅
Yeah sure, this could be tidied up. It'd need to be a major version release though, as it's potentially breaking if someone else is doing something along the lines of what you're doing with the route names.
x.x Yes, it's a potential breaking change.
But, it can be added a non-breaking (temporal) config option to replace this name, keeping the current value as default, so it will not be a BC and a normal feature release can be made, instead of a mayor release.
I'm planning to do a major release the next time I do work on this package (probably mid-May onwards), so will just include it in that.
https://github.com/laravel-json-api/laravel/issues/238