【问题标题】:Laravel - Insert multiple arrays as new rows?Laravel - 将多个数组作为新行插入?
【发布时间】:2021-12-12 10:35:14
【问题描述】:

下面给出的我的代码只向表中插入一行。对于$courseChapters 的chapter_content 数组中的每一组数据,我希望它作为CoursePublishChaptercontent 表中的新行添加,courseId 和points 字段的值对于此类数据的每一新行重复。对于示例,需要将 预期输出 中给出的 2 行新数据插入到表中,以用于下面给定的 var_export($courseChapters) 数据。有人可以告诉我如何实现这一目标吗?

foreach ($courseChapters['chapter_content'] as $chapter) {
     $postdata['chapterId']    =  $chapter['course_chapter_id'];
     $postdata['contentTypeId'] = $chapter['content_type_id'];
     $postdata['fileId']         = $chapter['file_id'];
                 
   $data=CoursePublishChaptercontent::create([
         'courseId' => $courseChapters['courseId'],
         'points'    =>  $courseChapters['points'],
         'course_chapter_id'=>$postdata['chapterId'],
         'content_type_id'=>$postdata['contentTypeId'],
         'file_id'=>$postdata['fileId']]);
 }

var_export($courseChapters) 是一个数组,如下所示:

array (
  'id' => 4,
  'courseId' => 24,
  'points' => 8,
  'chapter_content' => 
  array (
    0 => 
        array (
          'course_chapter_id' => 4,
          'file_id' => 3,
          'content_type_id' => 1,
        ),
    1 => 
        array (
          'course_chapter_id' => 4,
          'file_id' => 4,
          'content_type_id' => 2,
        ),
  ),
)

编辑

//$postdata=[];
                   $i=0;
                   foreach ($courseChapters['chapter_content'] as $chapter) {
                    $postdata[$i]['courseId'] = $courseChapters['courseId'];
                    $postdata[$i]['points']    =  $courseChapters['points'];
                    $postdata[$i]['course_chapter_id']    =  $chapter['course_chapter_id'];
                    $postdata[$i]['content_type_id'] = $chapter['content_type_id'];
                    $postdata[$i]['file_id']         = $chapter['file_id'];                              
                    $i++;
                }
         $data=CoursePublishChaptercontent::create($postdata);

预期/要求的输出:

[
  "courseId" => 24
  "points" => 8
  "course_chapter_id" => 4
  "content_type_id" => 1
  "file_id" => 3
],
[
  "courseId" => 24
  "points" => 8
  "course_chapter_id" => 4
  "content_type_id" => 2
  "file_id" => 4
]

当前输出:

array:2 [
  0 => array:5 [
    "courseId" => 24
    "points" => 8
    "course_chapter_id" => 4
    "content_type_id" => 1
    "file_id" => 1
  ]
  1 => array:5 [
    "courseId" => 24
    "points" => 8
    "course_chapter_id" => 4
    "content_type_id" => 1
    "file_id" => 3
  ]
]

【问题讨论】:

    标签: php laravel postgresql eloquent laravel-8


    【解决方案1】:

    您的$courseChapters 数组仅包含一个对象,因此只插入了 1 个数据。如果您仍想保持 $courseChapters 的数组格式为这样,此代码将起作用:

    foreach ($courseChapters as $courseChapter) {
       $courseId = $courseChapter["courseId"];
       $points = $courseChapter["point"]
       foreach ($courseChapter["chapter_content"] as $chapterContent) {
          $courseChapterId = $chapterContent["course_chapter_id"];
          $fileId = $chapterContent["file_id"];
          $contentTypeId = $chapterContent["content_type_id"];
          
          // Then put your create code below using the data provided above...
       }
    }
    

    就个人而言,我会反对这一点,因为那里有一个嵌套循环,我建议将您的 $courseChapters 的数组格式更改为与您的预期输出完全相同,因此,您只需要一个循环来插入记录.

    这是我提到的格式(如果你手动创建它):

    array (
       array (
         'id' => 4,
         'courseId' => 24,
         'points' => 8,
         'course_chapter_id' => 4,
         'file_id' => 3,
         'content_type_id' => 1
       ),
       array(
         'id' => 4,
         'courseId' => 24,
         'points' => 8,
         'course_chapter_id' => 4,
         'file_id' => 4,
         'content_type_id' => 2
       )
    );
    

    上面的格式是这样的:

    [
       [
          "id" => 4,
          "courseId" => 24,
          "points" => 8,
          "course_chapter_id" => 4,
          "file_id" => 3,
          "content_type_id" => 1
       ],
       [
          "id" => 4,
          "courseId" => 24,
          "points" => 8,
          "course_chapter_id" => 4,
          "file_id" => 4,
          "content_type_id" => 2
       ]
    ]
    

    【讨论】:

    • 您能告诉我如何将$courseChapters 的数组格式更改为与您要求的输出完全相同的预期输出
    • 这取决于您的前端发送数据,我假设$courseChapters 数组是您的前端发送到后端的数据。我只能添加我说的格式,会尽快更新答案。
    • 我在问你如何将$courseChapters 数组更改为与我的预期输出完全相同
    • 这个答案给出了错误Illegal string offset 'courseId'
    • 只需将代码中数组的键属性与您提供的数组的键属性重新对齐即可。我用您提供的信息检查了我的代码,它匹配。检查你的。
    猜你喜欢
    • 2018-12-09
    • 2020-05-01
    • 2020-04-08
    • 2017-12-26
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多