我有一个数组和另一个表的id。我需要每个数组项都是一个新记录。这些记录中的每一条都需要包括数组中的一项和另一个表的id。如何使store函数遍历数组,每次获取其他表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;
}
发布于 2020-09-04 04:29:20
既然你说它们中的每一个都是一个新的记录,这里有一个简单的方法来解释你所说的:
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();
}
https://stackoverflow.com/questions/63730671
复制相似问题