Laravel,Vue.js插入数组



我有一个数组和另一个表的id。我需要每个数组项都是一个新记录。这些记录中的每一条都需要包括数组中的一个项和另一个表的id。如何使存储函数在数组中循环,每次都获取另一个表id并插入数据库?

查看

<tablv>
<thead>
<tr>
<td>MFG</td>
</tr>
</thead>
<tbody>
<tr v-for="hp in ordered_handpieces">
<td>
<input type="checkbox" :value="hp.id" v-model="hp_id"><span>{{hp.mfg.mfg_name}}</span>
</td>
</tr>
</tbody>
</table>

脚本

data: function() {
return {
hp_wo_id:'',
hp_id:[],
}
},
methods: {
onSubmit(){
axios.post('/hp_wo_unit', this.$data)   

}    

}

控制器

public function store(Request $request){
foreach (request(['hp_id']) as $hp_id) {
hp_wo_unit::create(request(['hp_wo_id'],$hp_id));
}
return $request;
}

由于您说过它们中的每一个都是新记录,因此这里有一个简单的方法:

foreach (request(['hp_id']) as $item) {
hp_wo_unit::create(request(['hp_wo_id'],$item));
// Or 
$hp = new MODEL();
$hp->hp_wo_id = request(['hp_wo_id']);
$hp->hp_id = $item;
$hp-save();

}

最新更新