gpt4 book ai didi

php - Laravel 插入多个表和多个数据

转载 作者:行者123 更新时间:2023-11-29 18:25:45 25 4
gpt4 key购买 nike

我在使用 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

最佳答案

如果 content_titlesection_title 是逗号分隔的字符串,那么您需要在传递到 foreach 循环之前分解它:

$titles = explode(',',$request->get('content_title') );
foreach($titles as $index => $value){
//code
}

关于php - Laravel 插入多个表和多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46215759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com