2 回答
TA贡献1875条经验 获得超3个赞
好的,在我们在评论中来回讨论之后,我认为您正在寻找这样的东西:
$folders = $request->get('folder_detail', []);
foreach($folders as $folder) {
$record = Folder::firstOrNew(['id', $folder]);
$record->doctor_id = $request->id;
// You can add your own records here, or use the 'fill' function
}
因此,通过这种方式,您可以遍历所有文件夹。检查具有特定 ID 的文件夹是否已经存在,如果不存在,则创建一个新文件夹。该医生通过您记录中的doctor_id 链接。
TA贡献1772条经验 获得超5个赞
查找记录如果存在则更新否则创建
$post = $request->all();
$doctor = Doctor::find($post['id']);
if($doctor){
$doctor->update([
//your data for update doctor model
]);
//remove old folders which is related with this doctor
Folder::where('doctor_id', $doctor->id)->delete();
//add current data of folder
if(!empty($post['folder_detail'])){
foreach($post['folder_detail'] as $folder){
Folder::create([
'doctor_id' => $doctor->id,
'folder_detail' => $folder
]);
}
}
//return your response here
} else {
$doctor = Doctor::create([
//data for create doctore
]);
//add current data of folder
if(!empty($post['folder_detail'])){
foreach($post['folder_detail'] as $folder){
Folder::create([
'doctor_id' => $doctor->id,
'folder_detail' => $folder
]);
}
}
//return your response here
}
- 2 回答
- 0 关注
- 136 浏览
添加回答
举报