[英]How to restructure the array so is possible to store each answer and question_id in the answers table?
[英]How to get each question_id and answer from the array?
如果用戶在會議中進行注冊,並且正在注冊 1 名 ID 為“1”(Jake K)的注冊類型參與者和 1 ID 為“4”(John W)的注冊類型參與者,則請求表單數據有這 3 個數組:
"name" => array:2 [▼
1 => array:1 [▼
1 => "Jake"
]
4 => array:1 [▼
1 => "John"
]
]
"surname" => array:2 [▼
1 => array:1 [▼
1 => "K"
]
4 => array:1 [▼
1 => "W"
]
]
"answer" => array:1 [▼
1 => array:1 [▼
1 => array:2 [▼ // answers answered for the registration_type_id 1
1 => "answer1"
2 => "answer2"
]
]
]
有必要使用此數組的信息插入participants
和answers
表中。 它在participants
表中正確插入但對於answers
表不起作用:
foreach ($request->all()['name'] as $key => $nameArray) {
foreach ($nameArray as $nameKey => $name) {
// this is working
$participant_result = Participant::create([
'name' => $name,
'surname' => $request['surname'][$key][$nameKey],
'registration_id' => $registration->id,
'registration_type_id' => $key
]);
// save answer to Database if exist
// this is not working
$answer = Answer::create([
'question_id' => $request['answer'][$key], // the issue is here
'participant_id' => $participant_result->id,
'answer' => $request['answer'][$key][$nameKey], // and here
]);
}
}
問題是因為有必要插入到answers
表的question_id
和answer
列中。 但是這個值沒有從帶有“ $request['answer'][$key]
”和“ $request['answer'][$key][$nameKey]
”的數組中正確返回。
您知道如何從數組中正確獲取問題 ID 和答案嗎?
在上面的數組示例中,“answer1”的問題 id 為 1,“answer2”的 id 為 2。
"answer" => array:1 [▼
1 => array:1 [▼
1 => array:2 [▼
1 => "answer1"
2 => "answer2"
]
]
形式:
<form method="post"
action="https://proj.test/conference/1/conf-test/registration/storeRegistration">
<h6> Participant - 1 - general</h6>
<div class="form-group">
<label for="namegeneral_1">Name</label>
<input type="text" required id="namegeneral_1" name="name[1][1]" class="form-control" value="">
</div>
<div class="form-group">
<label for="surnamegeneral_1">Surname</label>
<input type="text" id="surnamegeneral_1" class="form-control" name=" surname[1][1]" value="">
</div>
<div class="form-group">
<label for="participant_question">Question 1</label>
<input type='text' name='answer[1][1][1]' class='form-control' required>
<input type="hidden" name="question_required[1][1]" value="1">
</div>
<div class="form-group">
<label for="participant_question">Question 2</label>
<textarea name='answer[1][1][2]' class='form-control' rows='3' required></textarea>
<input type="hidden" name="question_required[1][2]" value="1">
</div>
<h6> Participant - 1 - plus</h6>
<div class="form-group">
<label for="nameplus_1">Name</label>
<input type="text" required id="rnameplus_1" name="rname[4][1]" class="form-control" value="">
</div>
<div class="form-group">
<label for="surnameplus_1">Surname</label>
<input type="text" id="rsurnameplus_1" class="form-control" name=" rsurname[4][1]" value="">
</div>
<input type="submit" class="btn btn-primary" value="Register"/>
</form>
第一:在循環中,您有多個名字,但 answer 有一個鍵 ( 1
)。 在第二次運行時,您有$key == 4
,但在$request['answer']
。 也許在第一個循環中你應該有兩個循環? 一種用於注冊參與者,一種用於注冊答案? 如果兩個答案都注冊了,用$participantResult
構建數組,並為每個答案注冊count($participantResult)
答案:O 如果沒有,只記住第一個注冊參與者的 ID 並保存答案。
請告訴我$request['answer']
是如何工作的? 什么是$request['answer'][1]
什么是$request['answer'][1][1]
?
其次$request['answer'][$key][$nameKey]
在這種情況下是兩個元素的數組。 如果 Answer::create 知道它 - 沒問題;)
編輯
這是正確的解決方案嗎?
<?php
$aPID = [];
foreach ($request->all()['name'] as $key => $nameArray) {
foreach ($nameArray as $nameKey => $name) {
$aPID[$key] = Participant::create([
'name' => $name,
'surname' => $request['surname'][$key][$nameKey],
'registration_id' => $registration->id,
'registration_type_id' => $key
]);
}
}
foreach($request['answer'] as $pid => $answersArray) {
foreach($answersArray as $questionId => $answer) {
$answer = Answer::create([
'question_id' => $questionId,
'participant_id' => $aPID[$pid],
'answer' => $answer,
]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.