[英]Laravel, store multiple variables from array into DB
Im not sure if the question title is correct but what I wanna do is clear in the example below I have 3 arrays and I want to store data in one table我不确定问题标题是否正确,但在下面的示例中我想做的很清楚我有 3 个 arrays 并且我想将数据存储在一个表中
the table structure is like this表结构是这样的
table name: tour_transports表名:tour_transsports
id ID
transport_id transport_id
transport_track运输轨道
transport_note transport_note
arrays coming from blade arrays 来自刀片
"transport_id" => array:2 [
1 => "1"
5 => "5"
]
"transport_track" => array:3 [
1 => " from airport to the hotel"
3 => null
5 => " be ready at 4:pm"
]
"transport_note" => array:3 [
1 => "from hotel to the beach"
3 => null
5 => " bring ur swiming suite"
]
so as you see the ids are 1 and 5 so I wanna store two rows here所以你看到id是1和5所以我想在这里存储两行
id = 1,transport_id = 1, transport_track = from airport to the hotel, transport_note = be ready at 4:pm id = 1,transport_id = 1, transport_track = 从机场到酒店, transport_note = 4:pm 准备好
id = 2,transport_id = 5, transport_track = from hotel to the beach, transport_note = bring ur swiming suite id = 2,transport_id = 5, transport_track = 从酒店到海滩, transport_note = 带上你的游泳套房
I tried to do this but can't get it correct我试图这样做,但无法正确
if(!empty($request->transport_id))
{
foreach ($request->transport_id as $key => $transport_id) {
foreach ($request->transport_track as $key => $track) {
foreach ($request->transport_note as $key => $transport_note) {
TourTransport::create([
'transport_id' =>$transport_id,
'transport_track' =>$track[$transport_id],
'transport_note' =>$transport_note[$transport_id]
]);
}
}
}
}
You can use this code to achieve your results:您可以使用此代码来实现您的结果:
$data = $request->only('transport_id', 'transport_track', 'transport_note');
foreach ($data['transport_id'] as $key => $transportId) {
TourTransport::create([
'transport_id' => $transportId,
'transport_track' => $data['transport_track'][$key],
'transport_note' => $data['transport_note'][$key],
]);
}
Now $results contains the required data.现在 $results 包含所需的数据。 However the better solution might be to configure your html form to send related data in single array entry, Like:
然而,更好的解决方案可能是配置您的 html 表单以在单个数组条目中发送相关数据,例如:
<input type="text" name="transports[0][transport_id]">
<input type="text" name="transports[0][transport_track]">
<input type="text" name="transports[0][transport_note]">
<input type="text" name="transports[1][transport_id]">
<input type="text" name="transports[1][transport_track]">
<input type="text" name="transports[1][transport_note]">
Which you can also use Javascript/php loops to generate the form inputs.您还可以使用 Javascript/php 循环来生成表单输入。 Then you will receive form data in server side in well formed structure like:
然后您将在服务器端以格式良好的结构接收表单数据,例如:
"transports" => array:2 [
[
'transport_id' => 1,
'transport_track' => 'from airport to the hotel',
'transport_note' => 'be ready at 4:pm',
],
[
'transport_id' => 5,
'transport_track' => 'from hotel to the beach',
'transport_note' => ' bring ur swiming suite',
],
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.