belongsToMany
Usage
import { Model } from 'pinia-orm'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id')
}
}
}
With Decorator
import { Model } from 'pinia-orm'
import { Attr, BelongsToMany, Str } from 'pinia-orm/decorators'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@BelongsToMany(() => Role, () => RoleUser, 'user_id', 'role_id') declare roles: Role[]
// or if you have other pivot key
// @BelongsToMany(() => Role, { as: 'userPivot', model: () => RoleUser }, 'user_id', 'role_id')
}
Typescript Declarations
function belongsToMany(
related: typeof Model,
pivot: (() => typeof Model) | {
as: string
model: () => typeof Model
},
foreignPivotKey: string,
relatedPivotKey: string,
parentKey?: string,
relatedKey?: string,
): BelongsToMany
Table of Contents