attach method
Create a record in pivot table and return the id of last inserted row.
var classroom = Class();
var student = Student();
var query = await classroom.getStudents();
await query.attach(student);
Implementation
Future<int> attach(RelationshipModel model,
{Map<String, Object?>? extras}) async {
if (query == null) {
throw Exception('cannot query without relationship');
}
if (_pivotTable == null) {
throw Exception('Unknown Pivot table');
}
var initialParentValue = primaryValue;
var relatedParentValue = model.primaryValue;
Map<String, Object?> values = {
"$_initialForeignKey": initialParentValue,
"$_relatedForeignKey": relatedParentValue,
if (extras != null) ...extras,
};
var database = await eloquent.getDatabase;
return await database.insert(_pivotTable!, values);
}