在以下情况下,我需要一些帮助。
我愿意saveMany根据输入值。让我举一个例子。
我正在尝试以下方法。
$data = [
'id' => 1,
'name' => 'example',
'number_of_slots' => 5,
'material' => 'Colo',
'equipment_status_code_id' => 1,
];
$platecontainer = PlateContainer::create($data);
foreach ($data as $key => $value) {
$platecontainer->containerSlots()->saveMany([
new ContainerSlot([
'plate_container_id' => $data['id'],
'slot' => $data['number_of_slots'],
'occuiped' => false,
])
]);
}
Run Code Online (Sandbox Code Playgroud)
直到$platecontainer一切正常为止。我想要的是PlateContainer使用data数组创建a时,我也想创建插槽,但这是基于number_of_slots
因此,例如number_of_slots在示例中5,我想将5记录以(降序)保存在ContainerSlot表中
所以containerlots表最终看起来像这样。
所述节省许多方法接受的模型的阵列,所以只使用一个for环为$plateContainer的number_of_slots
$plateContainer = PlateContainer::create($data);
$containers = [];
// Need to add one to our number of slots because
// the $i count starts at one instead of zero.
$slots = $plateContainer->number_of_slots + 1;
for($i = 1; $i < $slots; $i++) {
$containers[] = new ContainerSlot([
'plate_container_id' => $plateContainer->getKey(),
'slot' => $i,
'occuiped' => false,
]);
}
$plateContainer->containerSlots()->saveMany($containers);
Run Code Online (Sandbox Code Playgroud)