Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

selcukgiray's avatar

Laravel REST API How to properly define nested routes for Post-User relationships?

Hi everyone,

I'm using Laravel 12 and building a REST API. I'm a bit confused about how to structure routes for users and posts relationships.

Currently, my route setup looks like this:

Route::apiResource("users", UserController::class);
Route::apiResource("users.posts", UserPostController::class)->only(["index"]);
Route::apiResource("posts", PostController::class);
Route::apiResource("posts.users", PostUserController::class)->only(["index"]);

My intentions are:

/users/{user}/posts → list all posts of a user

/posts/{post} → post details

/posts/{post}/user → the user who created the post (single owner)

Questions:

1- Is using /posts/{post}/users for a single owner REST-compliant?

2- What is the recommended route structure for the single owner of a post?

3- How can I express this properly in Laravel using apiResource and nested resources?

0 likes
4 replies
martinbean's avatar

@selcukgiray You could follow the examples in the JSON:API specification, where you have routes for interacting with relations of resources.

So, given your example, if a user can have many posts, then you would have dedicated to-many relationship endpoint to query those related posts:

  • GET /users/{user}/relationships/posts

If a post can only be authored by only one user, then you’d have a to-one relationship endpoint to get a post’s author:

  • GET /posts/{post}/relationships/author

Where the author may just be a User resource, but in the context of a post, you may call that user an “author” rather than just “user”.

selcukgiray's avatar

Thank you very much for your answer.

But how can I manage my API routes in the cleanest and most effective way? I don’t feel completely confident about the approach I’m using — it feels like I might be doing something wrong.

martinbean's avatar

@selcukgiray I don’t really understand what you’re asking? I just gave you an example of routes above.

Register the ones you actually need. If you only need to list posts for a user, then you can register the route individually:

Route::get('users/{user}/relationships/posts', [UserPostController::class, 'index']);
selcukgiray's avatar

I currently define my routes like this:

Route::prefix("users")->group(function() {
    Route::get("/", [UserController::class, "index"]);
    Route::post("/", [UserController::class, "store"]);
    Route::get("/{id}", [UserController::class, "show"]);
    Route::put("/{id}", [UserController::class, "update"]);
    Route::delete("/{id}", [UserController::class, "destroy"]);
    Route::get("/{id}/posts", [UserPostController::class, "index"]);
});

Route::prefix("posts")->group(function() {
    Route::get("/", [PostController::class, "index"]);
    Route::post("/", [PostController::class, "store"]);
    Route::get("/{id}", [PostController::class, "show"]);
    Route::put("/{id}", [PostController::class, "update"]);
    Route::delete("/{id}", [PostController::class, "destroy"]);
    Route::get("/{id}/author", [PostUserController::class, "show"]);
});

It works perfectly fine, but I’m not sure if this is the cleanest or most “Laravel way” to structure my routes.

Should I keep defining them manually like this, or would it be better to use Route::apiResource() and nested resources?

I’d love to know what’s considered best practice for organizing routes in a RESTful Laravel API.

Please or to participate in this conversation.