我在使用1表格将数据插入3表时遇到问题。 我想在保存节表后动态获取section_id。 section_id将放在内容表上。
这是我的代码
// insert course
$Course = new Course;
$Course->title = $request->title;
$Course->goal = $request->goal;
$Course->description = $request->description;
$Course->image = $request->image;
$Course->id_category = $request->id_category;
$Course->id_level_course = $request->id_level_course;
$Course->id_author = $request->id_author;
$Course->save();
$Course_id = $Course->id;
// insert course
// insert section
foreach($request->get('section_title') as $index => $value){
if($value != null){ // check not null request value
$Section = new Section;
$Section->title = $value;
$Section->id_course = $Course_id;
$Section->save();
$Section_id = $Section->id;
}
}
// insert section
// insert content
foreach($request->get('content_title') as $index => $value){
if($value != null){ // check not null request value
$Content = new Content;
$Content->title = $value;
$Content->id_section = $Section_id;
$Content->save();
}
}
// insert content
答案 0 :(得分:0)
如果content_title
和section_title
是逗号分隔字符串,那么在传递给explode
循环之前,您需要foreach
:
$titles = explode(',',$request->get('content_title') );
foreach($titles as $index => $value){
//code
}
答案 1 :(得分:0)
您可以在部分后插入每个内容。
$contents = $request->get('content_title')
foreach($request->get('section_title') as $index => $value){
if($value != null){ // check not null request value
$Section = new Section;
$Section->title = $value;
$Section->id_course = $Course_id;
$Section->save();
$Section_id = $Section->id;
// insert content
$title = isset($contents[$index]) ? $contents[$index]: null;
if($title != null){ // check not null request value
$Content = new Content;
$Content->title = $title;
$Content->id_section = $Section_id;
$Content->save();
}
}
}
//插入部分