douju6651 2016-02-10 11:27
浏览 25
已采纳

Laravel 5 - 保存存储在2d阵列中的数据

I have a form which takes the following structure

<tr id='actionRow'>
    <td>
        <input type="text" name='actionInput[0][]' id="actionInput"  placeholder='Action' class="form-control"/>
    </td>
    <td>
        <select class="responsibility" name="actionInput[1][]" id="responsibilityInput">
            <option value=""></option>
            @foreach($users as $user)
                <option value="{{ $user->userName }}">{{ $user->userName }}</option>
            @endforeach
        </select>
    </td>
    <td>
        <input type="text" name='actionInput[2][]' id="dateInput" placeholder='Completion Date' class="form-control dateControl"/>
    </td>
</tr>

I have an option for the user to add rows to this form, and it basically clones the table row. The end result I get in my controller when I output actionInput is something like the following

array:3 [▼
  0 => array:3 [▼
    0 => "Action Input 1"
    1 => "Action Input 2"
    2 => "Action Input 3"
  ]
  1 => array:3 [▼
    0 => "Responsibility Input 1"
    1 => "Responsibility Input 2"
    2 => "Responsibility Input 3"
  ]
  2 => array:3 [▼
    0 => "Date Input 1"
    1 => "Date Input 2"
    2 => "Date Input 3"
  ]
]

When this data is added to my database table, a table row should consist of one bit of data from each array. So if I take the above data and insert it into a database, my table rows should look like the following

Action  |  Responsibility   | Date
-------------------------------------
Input 1 |  Responsibility 1 | Date 1
-------------------------------------
Input 2 |  Responsibility 2 | Date 2
-------------------------------------
Input 3 |  Responsibility 3 | Date 3
-------------------------------------

In my controller I am currently doing this

$actions = Input::get('actionInput');

foreach($actions as $action => $category) {
    $contactReportActions = new ContactReportActionsDoc();
    $contactReportActions->contactReportId = $contactReport->id;
    foreach($category as $key => $value){
        $contactReportActions->action = $value;
        $contactReportActions->responsibility = $value;
        $contactReportActions->deliveryDate = $value;
    }
    $contactReportActions->save();
}

The problem is, that produces something like the following

Action           |  Responsibility   | Date
----------------------------------------------------------
Input 1          |  Input 2          | Input 3
----------------------------------------------------------
Responsibility 1 |  Responsibility 2 | Responsibility 3
----------------------------------------------------------
Date 1           |  Date 2           | Date 3
----------------------------------------------------------

So how could I correct this within my controller so it inputs the data to my database correctly?

Thanks

  • 写回答

1条回答 默认 最新

  • dtd14883 2016-02-10 11:48
    关注

    I would do something like this:

    <tr id='actionRow'>
        <td>
            <input type="text" name="actionInput[{{$key}}]['action']" id="actionInput"  placeholder='Action' class="form-control"/>
        </td>
        <td>
            <select class="responsibility" name="actionInput[{{$key}}]['responsibility']" id="responsibilityInput">
                <option value=""></option>
                @foreach($users as $user)
                    <option value="{{ $user->userName }}">{{ $user->userName }}</option>
                @endforeach
            </select>
        </td>
        <td>
            <input type="text" name="actionInput[{{$key}}]['date_input']" id="dateInput" placeholder='Completion Date' class="form-control dateControl"/>
        </td>
    </tr>
    

    In the view, you should generate a random or sequential key to the array, and store the table's columns with their names. For each row you clone later, you generate another key.

    This layout will also allow a easier entity edit if you need to.

    Then, in the Controller, you can separate each entity property like this:

        foreach (\Input::get('actionInput', array()) as $entityData)
        {
            $entity = ContactReportAction::find($entityData["id"]) ?: new ContactReportAction;
            $entity->fill($entityData);
            $entity->save();
        }
    

    Check if this helps you...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题