attachMany method
Create records in pivot table and return a list of added indexes.
var classroom = Class();
var query = await classroom.getStudents();
await query.attachMany([Student(),Student()]);
Implementation
Future<List<int>> attachMany(List<RelationshipModel> models,
{Map<String, Object?>? extras}) async {
if (query == null) {
throw Exception('cannot query without relationship');
}
if (_pivotTable == null) {
throw Exception('Unknown Pivot table');
}
if (models.isEmpty) {
throw Exception('Empty models');
}
var initialParentValue = primaryValue;
var database = await eloquent.getDatabase;
List<int> indexes = [];
for (var model in models) {
var relatedParentValue = model.primaryValue;
Map<String, Object?> values = {
"$_initialForeignKey": initialParentValue,
"$_relatedForeignKey": relatedParentValue,
if (extras != null) ...extras,
};
indexes.add(await database.insert(_pivotTable!, values));
}
return indexes;
}