0

我有以下型号:

服务类别:

'use strict'

const Model = use('Model')

class ServiceCategory extends Model {
  services() {
    return this
      .belongsToMany('App/Models/Service')
      .withTimestamps()
  }
}

module.exports = ServiceCategory
'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

class ServiceCategorySchema extends Schema {
  up () {
    this.create('service_categories', (table) => {
      table.increments()

      table.string('name')
      table.string('slug').index()
      table.text('description').nullable()
      table.string('icon').nullable()

      table.boolean('is_activated').default(false).notNullable().default(true)

      table.timestamps()

      table.timestamp('deleted_at').nullable()
    })
  }

  down () {
    this.drop('service_categories')
  }
}

module.exports = ServiceCategorySchema

服务:

'use strict'

const Model = use('Model')

class Service extends Model {
  categories() {
    return this
      .belongsToMany('App/Models/ServiceCategory')
      .withTimestamps()
  }
}

module.exports = Service
'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

class ServiceSchema extends Schema {
  up () {
    this.create('services', (table) => {
      table.increments()

      table.string('name')
      table.string('slug').index()
      table.text('description').nullable()
      table.string('icon').nullable()

      table.float('price', 10, 2)

      table.boolean('is_negotiable').default(true)

      table.boolean('is_activated').default(false).notNullable().default(true)

      table.timestamps()

      table.timestamp('deleted_at').nullable()
    })
  }

  down () {
    this.drop('services')
  }
}

module.exports = ServiceSchema

问题是,当我尝试使用他们的服务获取所有ServiceCategories时,我只会得到带有空服务数组的ServiceCategories :

const serviceCategories = await ServiceCategory
      .query()
      .with('services')
      .fetch()
[
    {
      "id": 1,
      "name": "Beleza",
      "slug": "beleza",
      "description": null,
      "icon": null,
      "isActivated": true,
      "createdAt": "2019-10-21T10:43:32.000Z",
      "updatedAt": "2019-10-21T10:43:32.000Z",
      "services": []
    }
]

中间表:

'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

class ServiceServiceCategorySchema extends Schema {
  up () {
    this.create('service_service_category', (table) => {
      table.increments()

      table.integer('service_id').unsigned().index()
      table
        .foreign('service_id')
        .references('id')
        .inTable('services')
        .onDelete('cascade')

      table.integer('service_category_id').unsigned().index()
      table
        .foreign('service_category_id')
        .references('id')
        .inTable('service_categories')
        .onDelete('cascade')

      table.timestamps()
    })
  }

  down () {
    this.drop('service_service_category')
  }
}

module.exports = ServiceServiceCategorySchema

但我可以将 a 附加service到 a service_category

const service = await Service.create(params)

await service.categories().attach([serviceCategoryId])

这成功地将与数据透视表上的servicea关联起来。service category

现在,为什么我能够添加关系但我无法加载它?

4

1 回答 1

2
'use strict'

const Model = use('Model')

class ServiceCategory extends Model {
  services() {
    return this
      .belongsToMany('App/Models/Service')
      .pivotTable("service_service_category")
      .withTimestamps()
  }
}

module.exports = ServiceCategory

添加pivotTable名称后尝试这个如果工作

于 2019-10-21T16:23:05.423 回答