Arrays 如何使用Laravel在数据库中保存多个表行
我希望能够在一次保存操作中存储多个数组值。 这可能吗 我有一个生成以下数组的动态表:Arrays 如何使用Laravel在数据库中保存多个表行,arrays,laravel,Arrays,Laravel,我希望能够在一次保存操作中存储多个数组值。 这可能吗 我有一个生成以下数组的动态表: array:5 [▼ "_token" => "cNvFpF7e7fVl0vDxPcCOlbH0eUadXIWaokwH7z3F" 5 => array:9 [▼ 0 => "1" 1 => "1" 2 => "0" 3 => &
array:5 [▼
"_token" => "cNvFpF7e7fVl0vDxPcCOlbH0eUadXIWaokwH7z3F"
5 => array:9 [▼
0 => "1"
1 => "1"
2 => "0"
3 => "2"
4 => "0"
5 => "3"
6 => "0"
7 => "0"
8 => "0"
]
4 => array:9 [▼
0 => "1"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
5 => "0"
6 => "0"
7 => "0"
8 => "0"
]
6 => array:9 [▶]
"save" => null
]
其中4=>、5=>、6=>表示用户ID和数组,以及它们各自的分数。
我不确定如何遍历多个数组来存储数据
DB表结构为id、用户id、游戏id、col-1、col-2等
希望,这是足够清楚,但如果你需要更多的信息,请让我知道。
谢谢
以下是表格代码:
这是我表格的代码:
@foreach($players as $p)
<tr class="">
<td class="h2 align-middle">
{{ $players->get($loop->index)->first_name }}
<input type="text" name="user_id" value="{{ $p->id }}">
</td>
@foreach($innings as $inning)
<td class="p-1">
<div class="form-group_{{$p->id}} input-group p-1 h4">
<select class="custom-select custom-select-lg py-0 prc" id="" name="player[{{$p->id}}]">
<option class="" value="0">0</option>
@foreach($scores as $score)
<option name="score[]" value="{{ $score }}">{{ $score }}</option>
@endforeach
</select>
</div>
</td>
@endforeach
<td>
<div class="h1 text text-center font-weight-bold pt-2">
<output id="result_{{$p->id}}"></output>
</div>
</td>
</tr>
@endforeach
@foreach($p)
{{$players->get($loop->index)->first_name}
@foreach($局数作为$局数)
0
@foreach($score作为$score)
{{$score}
@endforeach
@endforeach
@endforeach
雄辩的方法
Model::insert($data);
DB::table('table')->insert($data);
查询生成器方法
Model::insert($data);
DB::table('table')->insert($data);
使用上述方法,您可以在一个查询中插入多个条目
谢谢你。我理解这个概念,但是如何从我必须的格式获取数据到您指定的格式呢?这是我命名字段的方式吗?或者表单/表的结构或其他什么?您需要以与表中的列相同的方式定义数组的结构。您可以通过在for循环中运行它来轻松定义它。在for循环中运行insert查询比传递数组花费更多的时间。对于不理解您的响应,我深表歉意。我如何定义结构?我已将我的表代码添加到问题中。类似这样的操作您可以执行以下操作:
foreach($array as$val){$data[]=['user\u id'=>$val['user\u id'],'game\u id'=>$val['game-id']}