laravel icon indicating copy to clipboard operation
laravel copied to clipboard

[Q] Shouldn't be 'index' the route name for the listing action of a HasMany relationship?

Open nelson6e65 opened this issue 2 years ago • 7 comments

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?

nelson6e65 avatar Apr 11 '23 23:04 nelson6e65

It's been that way for a long time now, no one has ever raised it! Not sure it causes any problems?

lindyhopchris avatar Apr 12 '23 07:04 lindyhopchris

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.

nelson6e65 avatar Apr 12 '23 16:04 nelson6e65

I was automating/normalizing my http client in my Vue application and got confused when I got this inconsistency. 😅

nelson6e65 avatar Apr 12 '23 16:04 nelson6e65

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.

lindyhopchris avatar Apr 13 '23 08:04 lindyhopchris

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.

nelson6e65 avatar Apr 13 '23 17:04 nelson6e65

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.

lindyhopchris avatar Apr 22 '23 15:04 lindyhopchris

https://github.com/laravel-json-api/laravel/issues/238

nelson6e65 avatar May 05 '23 23:05 nelson6e65