用户填写表格以注册两个参加会议的人员并单击“注册”后,将出现一个包含请求数据的数组。
请求数据具有数组name
,该数组存储正在registration_type_id“ 1”(杰克)中注册的参与者的姓名和正在registration_type_id“ 4”(John)中注册的参与者的姓名。
与surname
数组相同,此姓氏数组存储每种注册类型的每个姓氏。
存储每个注册类型的每个答案的answer
数组也是如此。
"name" => array:2 [▼
1 => array:1 [▼
1 => "Jake"
]
4 => array:1 [▼
1 => "John"
]
]
"surname" => array:2 [▼
1 => array:1 [▼
1 => "W"
]
4 => array:1 [▼
1 => "K"
]
]
"answer" => array:1 [▼
1 => array:1 [▼
1 => array:6 [▼
1 => "answer1"
2 => "answer2"
]
]
]
疑问::我的疑问是如何从上述数组结构中获取信息,以便将每个参与者存储在参与者表中,以及如何将每个答案存储在答案表中。
参与者表上方的数组内容应保持不变:
id registration_type_id name surname registration_id
1 1 Jake W 1
2 4 John K 1
答案表应保持不变:
id participant_id question_id answer
1 1 1 answer1
1 1 2 answer2
您知道如何正确实现吗?
我有下面的代码,该代码首先在注册表中插入一个条目来存储注册:
# user object
$user = Auth::user();
# add registration to Database
$registration = Registration::create([
'conference_id' => $id,
'user_that_did_registration' => $user->id]
);
但是随后有必要将每个参与者存储在参与者表中,并将每个答案存储在答案表中。您知道如何正确地从上述数组中获取必要的信息,以使用以下代码存储在参与者表和答案表中吗?
要在参与者表中插入,必须使用:
$participant_result = Participant::create([
'name' => how to get the name of each participant from the array?,
'surname' => how to get the surname of each participant from the array?,
'registration_id' => $registration->id,
'registration_type_id' => how to get the registration_type_id from the array?
]);
要在答案表中插入,必须使用:
$answer = Answer::create([
'question_id' => how to get each question id from the array?,
'participant_id' => $participant_result->id,
'answer' => how to get each answer from the array?
]);