在以下情况下,我需要一些帮助。
我愿意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, ]) ]); }
直到$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);