0

Have try to create and nested relation with adonisjs.

Invoice -> hasMany -> InvoiceDetails -> belongsTo -> product

This is the Laravel query that works!

Invoice::with('invoiceDetails.product')->get();

These are my relations

Invoice Model

@hasMany(() => InvoiceDetail,{foreignKey: 'invoice_id'})
public invoiceDetails: HasMany<typeof InvoiceDetail>

InvoiceDetails Model

@belongsTo(() => Product,{foreignKey: 'product_id'})
public product: BelongsTo<typeof Product>

Invoice Controller

const invoice = await Invoice.query().preload('invoiceDetails').where('id', request.param('id')).firstOrFail();

How to solve this?

1 Answers1

-1

I found the solutions.but i don't know about the solutions is best.

there is my solutions

 const invoice = await Invoice
  .query()
  .preload('invoiceDetails', (profileQuery) => {
    profileQuery.preload('product')
  })
  .where('id', request.param('id'))
  .firstOrFail();
andreyunugro
  • 1,096
  • 5
  • 18
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 07 '21 at 22:30