How to get parent (parentID ==0) and his all children (parentID == ID )?

Hello guys,
How to get all parent Services (parentID ==0) and each Services with his children (parentID == servicesID ) ?

Thank you in advance !

// In your controller do:

const ParentService = use('App/Models/ParentService')

class ParentController {
  async show({request, response}) {
    const { parentID } = request.post()
    const parentService = await ParentService.query()
    .where('id', parentID)
    .with('children')
    .fetch()
    
    return response.send(200).json({
      message: 'Query successful',
      data: parentService
    })
  }
}



// This assumes that you have created the 'children' relation in your 'ParentService' model

// In your 'ParentService' model, you should have:

class ParentService extends Model {
  children() {
    return this.hasMany('App/Models/ChildrenService')
  }
}


Thank you for your answer.

THIS IS MY MODEL Productcategorie.js

‘use strict’

/** @type {typeof import(’@adonisjs/lucid/src/Lucid/Model’)} */

const Model = use(‘Model’)

class Productcategorie extends Model {

products() {

    return this.hasMany('App/Models/Product')

}

}

module.exports = Productcategorie

The concerning method (do not work) in my controller :
async homeIndex({ params, request, response, view }) {

            const productcategorie = await Productcategorie.query().with('products').fetch()

    return view.render('site/index', {

        productcategories: productcategorie.toJSON(),

    })

}

In my database: productcategories table’s PRIMARY KEY is CategorieID witch is also FOREIGN KEY in the products table

What i’m trying to do : Get all productcategories with their products, to display then like this:

ProductCategory_1:

  • Product_1

  • Product_2

  • Product_3

  • Product_N

ProductCategory_2:

  • Product_1
  • Product_2
  • Product_3
....
  • Product_N

ProductCategory_N:

  • Product_1

  • Product_2

  • Product_3

  • Product_N

Can you elaborate on “do not work”?