Php Laravel数组如何保存到数据库
问题是,我想将该数组存储到数据库中,以便像这样,主题可以是或多或少的 数据库选择表Php Laravel数组如何保存到数据库,php,laravel,eloquent,Php,Laravel,Eloquent,问题是,我想将该数组存储到数据库中,以便像这样,主题可以是或多或少的 数据库选择表 |----|---------|----------|------------------| | id | user_id | topic_id | question_number | |----|---------|----------|------------------| | 1 | 1 |1st array | 21 | |----|---------|----
|----|---------|----------|------------------|
| id | user_id | topic_id | question_number |
|----|---------|----------|------------------|
| 1 | 1 |1st array | 21 |
|----|---------|----------|------------------|
| 2 | 1 |2nd array | 5 |
|----|---------|----------|------------------|
| 3 | 1 |3rd array | 3 |
|----|---------|----------|------------------|
..............................................
| 34 | 1 |34 array | 5 |
|----|---------|----------|------------------|
dd
array:3 [▼
"_token" => "HhR1wmjgJUvmnRsoYxcg3yezqzq7ORKqX6dAHzCD"
"time" => "30:01"
"number" => array:34 [▼
1 => "21"
2 => "5"
3 => "3"
4 => "0"
5 => "0"
6 => "0"
........
34 => "5"
]
控制器存储功能
public function store(Request $request){
$time = $request->input('time');
foreach ($request->input('number') as $key => $value) {
Choice::create([
'user_id' => Auth::id(),
'time' => $time,
'topic_id' => $key,
]);
}}
你有什么错误吗?没有,因为我没有发现如何获取数组数据。但我还在搜索你有什么错误吗?没有,因为我没有找到如何获取数组数据。但我仍然在搜索什么是dd($request->input('number');当然。我只是等了10分钟。谢谢你接受你的回答。谢谢。您不需要保存'topic\u id'=>1或'topic\u id'第一数组'?;不,我能处理。我猜。什么是dd($request->input('number');当然。我只是等了10分钟。谢谢你接受你的回答。谢谢。您不需要保存'topic\u id'=>1或'topic\u id'第一数组'?;不,我能处理。我想是吧。
public function store(Request $request) {
$time = $request->input('time');
foreach ($request->input('number') as $key => $value) {
Choice::create([
'user_id' => Auth::id(),
'time' => $time,
'topic_id' => $key, // or $key . 'st array';
'question_number' => $value,
]);
}
}}