adonisjs many to many create pivot 1

adonisjs many to many create pivot

import { BaseModel, manyToMany, ManyToMany } from '@ioc:Adonis/Lucid/Orm'
import Project from 'App/Models/Project'

class User extends BaseModel {
  @manyToMany(() => Project)
  public projects: ManyToMany
}

Here is what the above code is Doing:
1. We are importing the manyToMany and ManyToMany from the @ioc:Adonis/Lucid/Orm module.
2. We are defining a manyToMany relationship between the User and Project models.
3. We are defining a projects property on the User model, which will be used to fetch the related projects.

Now, let’s see how to use the manyToMany relationship.

Fetching related projects

You can fetch the related projects by using the projects property on the User model.

Similar Posts