with_() goes on a query. It loads the authors for every comment it finds in one extra query, whether there are 2 comments or 200. load() does the same for a model you already have.
Reading comment.author without loading it first raises a LazyLoadingViolation. Loading it quietly would turn a loop over 200 comments into 200 extra queries, and you wouldn't notice until the page got slow.
Change show() in IssueController to return the detail resource:
python
from app.http.resources.issue import IssueResource from app.http.resources.issue import IssueDetailResource, IssueResource class IssueController: ... async def show(self, issue: int) -> IssueResource: found = await Issue.find_or_fail(issue) await found.load("reporter", "assignee") return IssueResource.model_validate(found) async def show(self, issue: int) -> IssueDetailResource: found = await Issue.with_("reporter", "assignee", "comments.author").find_or_fail(issue) return IssueDetailResource.model_validate(found)
"comments.author" is a nested path. It loads the issue's comments and then the author of each comment, with one query per level however long the thread is.
Nested paths need with_()
Use with_() on the query for nested paths like comments.author. load() on a model you already have takes plain relationship names, such as load("reporter", "assignee").
uv run python artisan db:migrate:freshuv run python artisan db:seedcurl -X POST http://127.0.0.1:8000/api/projects/1/issues \ -H "Content-Type: application/json" \ -d '{"title": "Homepage hero image is blurry", "reporter_id": 1}'
Comment on it:
bash
curl -X POST http://127.0.0.1:8000/api/issues/1/comments \ -H "Content-Type: application/json" \ -d '{"author_id": 2, "body": "I can reproduce this on Safari."}'
json
{ "id": 1, "body": "I can reproduce this on Safari.", "author": {"id": 2, "name": "Grace Hopper", "email": "grace@example.com"}, "created_at": "2026-11-06T19:10:39.707099Z"}
Add a reply from Ada, with "author_id": 1, then fetch the issue. The thread comes with it, each comment with its author:
bash
curl http://127.0.0.1:8000/api/issues/1
json
{ "id": 1, "key": "WEB-1", "title": "Homepage hero image is blurry", ... "comments": [ {"id": 1, "body": "I can reproduce this on Safari.", "author": {"id": 2, "name": "Grace Hopper", ...}, ...}, {"id": 2, "body": "Thanks, swapping in a sharper image.", "author": {"id": 1, "name": "Ada Lovelace", ...}, ...} ], "transitions": ["in_progress"]}
An unknown author and an empty body are refused:
json
{ "message": "The selected author id is invalid. (and 1 more error)", "errors": { "author_id": ["The selected author id is invalid."], "body": ["The body field is required."] }}
Comments and eager loading
Comments let people discuss an issue. In this lesson you add them, then fetch an issue together with its whole thread.
1. Create the Table
2. Add the Model
Create
app/models/comment.py:And give
Issueits comments inapp/models/issue.py:3. Validate and Shape Comments
Create
app/http/requests/comment.py:And
app/http/resources/comment.py:The issue list stays small, but a single issue should come with its thread. Add a detail resource to
app/http/resources/issue.py:4. Add the Controller
Create
app/http/controllers/comment_controller.py:5. Load Related Rows Up Front
Relationships aren't loaded until you ask for them. There are two ways to ask:
with_()goes on a query. It loads the authors for every comment it finds in one extra query, whether there are 2 comments or 200.load()does the same for a model you already have.Reading
comment.authorwithout loading it first raises aLazyLoadingViolation. Loading it quietly would turn a loop over 200 comments into 200 extra queries, and you wouldn't notice until the page got slow.6. Load the Thread with the Issue
Change
show()inIssueControllerto return the detail resource:"comments.author"is a nested path. It loads the issue's comments and then the author of each comment, with one query per level however long the thread is.Nested paths need with_()
Use
with_()on the query for nested paths likecomments.author.load()on a model you already have takes plain relationship names, such asload("reporter", "assignee").7. Add the Routes
In
routes/api.py:Check
Start from a clean database with one issue:
Comment on it:
Add a reply from Ada, with
"author_id": 1, then fetch the issue. The thread comes with it, each comment with its author:An unknown author and an empty body are refused:
Compare your code
This lesson's code is tagged
lesson-10. See the changes in this lesson.