You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following model:
@manyToMany(() => Actor, { pivotTable: 'actor_matter', pivotColumns: ['relation', 'primary'], onQuery: (query) => { query.wherePivot('relation', 'client').wherePivot('primary', true) }, })
In the controller I have a show method that loads the relation:
await matter.load('client'})
However, this returns an array and I only want the first record.
I expected to be able to chain on .first() to either the model or controller query but neither works.
Have tried in the controller:
await matter.load('client', (query) => { query.first() })
And in the model I tried
.wherePivot('primary', true).first()
The error message returned is :
err: { "type": "TypeError", "message": "Cannot read properties of undefined (reading 'method')", "stack": TypeError: Cannot read properties of undefined (reading 'method')
How do I return just the first object in the array using Adonis ORM or query?
Note: Just to be clear, I realise I can get the record separately, i.e.
const client = await matter .related('actors') .query() .wherePivot('relation', 'client') .wherePivot('primary', true) .first()
But I want to return the client as part of the matter JSON data, so client will be accessible via
matter.client
.Beta Was this translation helpful? Give feedback.
All reactions