duangangpin078794 2019-05-11 02:01
浏览 187

如何使用laravel请求处理数组?

I am using the simple form and array input like this (not dynamic)

<input type="text" name="pre_ref_position[]" id="pre_ref_position">
<input type="text" name="pre_ref_position[]" id="pre_ref_position">

Form post with ajax and return data like this;

[
    0:{pre_ref_position:'example1'}
    1:{pre_ref_position:'example2'}
]

So I want to save this data with eloquent but how can I handle this array and save each row in the database?

  • 写回答

1条回答 默认 最新

  • duanpanbo9476 2019-05-11 02:35
    关注

    Assuming you receive your array in a variable called $refValin your controller

    Then use the following code:

    $refArr = [];
    foreach($refVal as $key => $val)
    {
      $now = Carbon::now();
      //ref_pos in the array below refers to the field name in your database.
      $refArr[] = [ 'ref_pos' => $val['pre_ref_position'] ,'created_at' => $now, 'updated_at' => $now ];
    }
    
    
    //assuming model name is `Position`
    Position::insert($refArr);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序