When I query model using Eager Loading, ex:
const user = await User.query().with('posts').fetch()
I wish I could choose only some fields.
In this sample, I will get all _postModel_ fields but only _post.title_ interests me.
Something like this:
const user = await User
.query()
.with('posts', builder => {
builder.select('title')
})
.fetch()
You also need to define the relation field.
const user = await User.query()
.with('posts', builder => {
builder.select('user_id', 'title'); // user_id is needed
})
.fetch();
Hey @alexhenriquepv, @sorxrob! 馃憢
The current solution is to use what @sorxrob proposed.
I believe we could make it more elegant by copying what Laravel has done.
Before
const user = await User.query()
.with('posts', builder => {
builder.select('user_id', 'title'); // user_id is needed
})
.fetch();
After
const user = await User.query()
.with('posts:title')
.fetch();
Closing since not actionable with the new release.
Most helpful comment
You also need to define the relation field.