So, why is this an issue? Because relationships do not show easily until toJSON() is called on individual rows and its a PITA once you get to the view.
Controller Model pull:
var results = await ResultModel
.query()
.orderBy('created_at','desc')
.with('test', (builder) => {
builder.select('id', 'name')
})
.with('testset',(builder) => {
builder.select('id', 'name')
})
.paginate(page, 10)
This returns results as an object containing something like below:
{
rows: {
Result {
__setters__: [Array],
'$attributes': [Object],
'$persisted': true,
'$originalAttributes': [Object],
'$relations': [Object],
'$sideLoaded': {},
'$parent': null,
'$frozen': false,
'$visible': undefined,
'$hidden': undefined
}
}
pages: { total: 20, perPage: 10, page: 1, lastPage: 2 },
isOne: false
}
I'm having to loop through result rows to repack serialized data in the controller. Since Edge doesn't have the ability to use variables (or so i think?) its difficult to solve this cleanly.
For now, I may opt to just use good old SQL and .fetch() to solve this issue.
PS: I do know I can do this: results.rows[0].$relations.testset.id but that's ugly
PPS: Adonis has been very nice! I've done Laravel work before and its so natural!
You can simply call results.toJSON() and you will get the following structure back.
{
page: 1,
total: 20,
perPage: 10,
lastPage: 2,
data: []
}
The data property will be an array of rows, that you can loop over.
Closing since no answer from issue reporter.
Most helpful comment
You can simply call
results.toJSON()and you will get the following structure back.The
dataproperty will be an array of rows, that you can loop over.