-
I have two tables that are from M to M, so there is a third table.
Error: --> TypeError: row.save is not a function
Error --> TypeError: one.save is not a function in the Employee Model.ts file, I have a relationship
and from many to many
the EmployeeHasSpecialty.ts file How can I insert using the "related" and "saveMany" relationship? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello Andre, @manyToMany(() => Specialty, { pivotTable: 'employee_has_specialty' })
public specialities: ManyToMany<typeof Specialty> and in your Controller you'll have to save first then attach specialities like this: const employee = new Employee()
const payload = await request.validate(EmployeeValidator)
// or the way you get your data
const newEmployee = await employee.fill(payload).save()
await newEmployee.related('specialities').attach(payload.specialities) in the update method make sure to detach before attach const employee = await Employee.findOrFail(params.id)
const payload = await request.validate(EmployeeValidator)
// or the way you get your data
await employee.merge(payload).save()
await employee.related('specialities').detach()
await employee.related('specialities').attach(payload.specialities) Hope it helps |
Beta Was this translation helpful? Give feedback.
Hello Andre,
in your Model you just have make it like that:
and in your Controller you'll have to save first then attach specialities like this:
in the update method make sure to detach before attach