ホーム>source

ユーザーが会議で登録を行い、登録フォームがこのデータを収集する場合:

"name" => array:1 [▼
    1 => array:2 [▼
      1 => "Jake"    // name of the participant being registered in the registration_type_id 1
      2 => "John"    // name of the participant being registered in the registration_type_id 1
    ]
  ]
  "surname" => array:1 [▼
    1 => array:2 [▼
      1 => "W"  // surname off 1st participant being registered in the registration_type_id 1
      2 => "K" // surname off 2nd participant being registered in the registration_type_id 1
    ]
  ]
  "answer" => array:1 [▼
    1 => array:2 [▼
      1 => array:2 [▼ // answers of the 1st participant being registered in the registration_type_id 1
        1 => "answer1p1"
        2 => "answer2p1"
      ]
      2 => array:2 [▼ // answers of 2nd participant being registered in the registration_type_id 1
        1 => "answer1p2" 
        2 => "answer2p2"
      ]
    ]
  ]

次に、参加者情報を参加者テーブルに保存し、各参加者に関連付けられた回答を回答テーブルに保存する必要があります。参加者テーブルへの挿入は正常に機能しています。

問題: 回答テーブルに挿入することは正しく機能していません。データベースには次のように保存する必要があります。

id  |    participant_id    |    question_id    |    answer
159 |       153            |         1         |    answer1p1
161 |       153            |         2         |    answer2p1
163 |       154            |         1         |    answer1p2
165 |       154            |         2         |    answer2p2 

しかし、次のように保存されます:

id  |    participant_id    |    question_id    |    answer
159 |       153            |         1         |     answer1p1
161 |       153            |         1         |    answer1p2
163 |       154            |          1        |    answer1p1
165 |       154            |          1        |    answer1p2  
160 |       153            |          2        |    answer2p1
162 |       153            |          2        |    answer2p2
164 |       154            |          2        |    answer2p1
166 |       154            |          2        |    answer2p2

参加者と回答の表に挿入するコード:

foreach ($request->all()['name'] as $key => $nameArray) {
    foreach ($nameArray as $nameKey => $name) {
        $participant_result = Participant::create([
            'name' => $name,
            'surname' => $request['surname'][$key][$nameKey],
            'registration_id' => $registration->id,
            'registration_type_id' => $key
        ]);
        if (isset($request['answer'][$key])) {
            foreach ($request->all()['answer'][$key] as $rID => $answers) {
                foreach ($answers as $question_id => $answer) {
                    $answer = Answer::create([
                        'question_id' => $question_id,
                        'participant_id' => $participant_result->id,
                        'answer' => $answer,
                    ]);
                }
            }
        }
    }
}

あなたの答え
  • 解決した方法 # 1

    参加者ごとに常にすべての回答を追加しますが、自分の回答のみを追加する必要があります。 おそらく次のようなものを試すことができます:

    foreach ($request['name'] as $reg_type => $nameArray) 
    {
        foreach ($nameArray as $nameKey => $name) 
        {
            $participant_result = Participant::create([
                'name' => $name,
                'surname' => $request['surname'][$reg_type][$nameKey],
                'registration_id' => $registration->id,
                'registration_type_id' => $reg_type
            ]);
            if (isset($request['answer'][$reg_type][$nameKey])) 
            {
                foreach ($request['answer'][$reg_type][$nameKey] as $question_id => $answer) 
                {
                    $answer = Answer::create([
                        'question_id' => $question_id,
                        'participant_id' => $participant_result->id,
                        'answer' => $answer,
                    ]);
                }
            }
        }
    }
    
    

関連記事

  • 前へ java - JPAクエリ:サブクエリをグループ化条件に結合する
  • 次へ excel - 特定のスパンクラスに対してInnerTextが空を返す