如何使用 Laravel-Excel 循环读取电子表格中的所有列

How to loop read all the column from the spreadsheet using Laravel-Excel

我有一个包含测验记录的电子表格,我将所有 quiz_questions 和选项保存在数据库中。

我有这段代码读取并保存电子表格,但它有一些问题,我想保存里面的 choice_1、choice_2、choice_3 和 choice_4带有选项列名称的数据库。当我 运行 这段代码时,它只保存 choice_4 列而不是所有的选择。我怎样才能得到所有的选择并保存呢?谁能帮我?提前致谢。

这是我的 QuizImport

public function collection(Collection $rows)
{
    foreach($rows as $row){
        $quizQuestions = quizQuestions::create([
        'quiz_id' => $row['quiz_id'],
        'question_num' => $row['question_num'],
        'question_content' => $row['question_content'],
        'tagalog_question'=> $row['tagalog_question'],
        ]);

        $quizQuestions->choices()->create([
            'option' => $row['option_1'] || $row['option_2'] || $row['option_3'] || $row['option_4'],
            'remarks' => $row['remarks_1'] || $row['remarks_2'] || $row['remarks_3'] || $row['remarks_4'],    
            'tagalog_choices'=> $row['tagalog_choices'],
        ]);

    }
}

这是我的控制器

    public function store(Request $request){

    $file = $request->file('file');
    Excel::import(new QuizImport, $file);

      
    return response(['message'=>"Quiz successfully imported",
    'error'=>false,
    'error code'=>200,
    'line'=>"line".__LINE__."".basename(__LINE__),
    'file'=>$file],200,[],JSON_NUMERIC_CHECK);
}

This is the sample structure of my excel

这是预期的输出

    "quizQuestions": [
    {
        "id": 2,
        "quiz_id": 6,
        "question_num": 1,
        "question_content": "<p>Sample Question</p>",
        "correct_answer": null,
        "created_by": 7,
        "updated_by": null,
        "created_at": "2021-09-06T07:27:20.000000Z",
        "updated_at": "2021-10-18T17:35:17.000000Z",
        "question_image": null,
        "tagalog_question": "<p><span style=\"color: black;\">Tagalog Translation of the question</span></p>",
        "choices": [
            {
                "id": 2,
                "quiz_questions_id": 2,
                "option": "choices_1",
                "remarks": 1,
                "created_at": "2021-09-06T07:27:48.000000Z",
                "updated_at": "2021-10-18T17:53:13.000000Z",
                "choices_image": null,
                "tagalog_choices": "Tagalog"
            },
            {
                "id": 3,
                "quiz_questions_id": 2,
                "option": "choices_2",
                "remarks": 0,
                "created_at": "2021-09-06T07:28:01.000000Z",
                "updated_at": "2021-10-18T17:53:25.000000Z",
                "choices_image": null,
                "tagalog_choices": "Tagalog"
            },
            {
                "id": 4,
                "quiz_questions_id": 2,
                "option": "choices_3",
                "remarks": 0,
                "created_at": "2021-09-06T07:28:54.000000Z",
                "updated_at": "2021-10-18T17:53:32.000000Z",
                "choices_image": null,
                "tagalog_choices": "Tagalog"
            },
            {
                "id": 5,
                "quiz_questions_id": 2,
                "option": "choices_4",
                "remarks": 0,
                "created_at": "2021-09-06T07:29:07.000000Z",
                "updated_at": "2021-10-18T17:53:37.000000Z",
                "choices_image": null,
                "tagalog_choices": "Tagalog"
            }
        ]
    },

但这就是我得到的

  "quizQuestions": [
    {
        "id": 541,
        "quiz_id": 33,
        "question_num": 1,
        "question_content": "Sample Question",
        "correct_answer": null,
        "created_by": null,
        "updated_by": null,
        "created_at": "2022-01-10T11:35:42.000000Z",
        "updated_at": "2022-01-10T11:35:42.000000Z",
        "question_image": null,
        "tagalog_question": "Tagalog Translation of the Question",
        "choices": [
            {
                "id": 1735,
                "quiz_questions_id": 541,
                "option": "choice4",
                "remarks": 0,
                "created_at": "2022-01-10T11:35:42.000000Z",
                "updated_at": "2022-01-10T11:35:42.000000Z",
                "choices_image": null,
                "tagalog_choices": "Tagalog"
            }
        ]
    },

我相信您需要一个循环来为同一个父对象创建多个选择。 (未测试)

public function collection(Collection $rows)
{
    foreach($rows as $row){
        $quizQuestions = quizQuestions::create([
        'quiz_id' => $row['quiz_id'],
        'question_num' => $row['question_num'],
        'question_content' => $row['question_content'],
        'tagalog_question'=> $row['tagalog_question'],
        ]);

        for($i = 1; $i < 5; $++) {
            $quizQuestions->choices()->create([
                'option' => $row['option_' .$i],
                'remarks' => $row['remarks_' . $i],    
                'tagalog_choices'=> $row['tagalog_choices'],
            ]);
        }
    }
}