Similar to the question here but for D8. I've managed to create the child paragraphs but failing to generate the parent paragraphs and attaching the child to them.
$questions = [];
foreach($data['questions'] as $question) {
$answers = [];
foreach($question['answers'] as $answer) {
$correct = $answer['field_correct'] === 'Yes' ? 1 : 0;
$answers[] = Paragraph::create([
'type' => 'quiz_answers',
'field_answer' => $answer['field_answer'],
'field_correct' => $correct,
])->save();
}
$questions[] = Paragraph::create([
'type' => 'quiz_question',
'field_question' => $question['field_question'],
'field_answers' => $answers,
])->save();
}
$new_submission = Node::create([
'type' => 'quiz_submissions',
'title' => 'Quiz Submission',
'field_quiz' => $data['quizID'],
'field_question_submission' => $questions,
]);