How i can do this relationship?

Hi, nowadays i have this scene:

I have this records in table Quiz:

table.integer('student_id').references('id').inTable('student')

      table.integer('class_id').references('id').inTable('class')

      table.integer('book_unit_id').references('id').inTable('book_unit')

      table.date('start_date_time').defaultTo()

      table.date('end_date_time').defaultTo()

      table.string('execution_back_status')

      table.string('execution_app_status')

      table.boolean('send_back_status').defaultTo(false)

      table.integer('user_id').references('id').inTable('users')

So, i need to group this quizz by the class_id from table class, this is the schema:

table.string('code').notNullable()

      table.string('description')

      table.date('start_date')

      table.date('end_date')

      table.string('period')

      table.string('language')

      table.boolean('status').defaultTo(false)

      table.integer('user_id').references('id').inTable('users')

So i need this json:

[
  {
    "id": 13,
    "code": "ING-NOT-2019",
    "description": "Inglês Noturno 2019",
    "start_date": "2019-12-30T03:00:00.000Z",
    "end_date": "2019-12-31T03:00:00.000Z",
    "period": "Noturno",
    "language": "Inglês",
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-30 10:04:47",
    "updated_at": "2020-01-05 16:08:00",
    "language_substring": "US",
    "quiz": [
      {
        "id": 5,
        "book_id": 33,
        "unit": 1,
        "sequence": 1,
        "description": "UNIT_01_GRAMMAR",
        "qt_question": 5,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-27 08:11:21",
        "updated_at": "2019-12-30 14:54:12",
        "miniature": null
      },
      {
        "id": 6,
        "book_id": 33,
        "unit": 1,
        "sequence": 2,
        "description": "UNIT_01_VOCABULARY",
        "qt_question": 5,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-27 08:11:39",
        "updated_at": "2019-12-27 08:11:39",
        "miniature": null
      },
      {
        "id": 7,
        "book_id": 33,
        "unit": 2,
        "sequence": 1,
        "description": "UNIT_02_GRAMMAR",
        "qt_question": 5,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-27 08:11:46",
        "updated_at": "2019-12-27 08:11:46",
        "miniature": null
      }
    ]
  },
  {
    "id": 14,
    "code": "ESP-MAN-2019",
    "description": "Espanhol manhã 2019",
    "start_date": "2019-12-30T03:00:00.000Z",
    "end_date": "2019-12-31T03:00:00.000Z",
    "period": "Manhã",
    "language": "Espanhol",
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-30 11:06:44",
    "updated_at": "2019-12-30 11:06:44",
    "language_substring": null,
    "quiz": [
      {
        "id": 8,
        "book_id": 39,
        "unit": 1,
        "sequence": 1,
        "description": "UNIT_01_GRAMMAR",
        "qt_question": 5,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-30 11:07:09",
        "updated_at": "2019-12-30 15:03:50",
        "miniature": null
      }
    ]
  }
]

How i can do this?

1 Like

hi please test this code

  //put this snippet code in student Model
  quizzes () {
    return this.hasMany('App/Models/Quiz')
  }


  // put this snippet code in  Quiz Model

  user() {
    return this.belongsTo('App/Models/student')
  }


  // put this snippet code in your controller
  const getstudent = await student.query().with('quizzes').where('id', student_id).first()
1 Like