php—如何通过laravel在mysql中存储html表中的数据

mzillmmw  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(299)

我有一个动态排序表,我可以添加或删除行,如输入图像描述这里
在此处输入链接说明
现在我正在努力将这些数据插入我的数据库(mysql)。
这是我的查看页面

<table class="table table-hover table-sortable" id="tab_logic" name="DataTable">
    <thead>
    <tr class="text-center">
        <td style="width: 40%;">Process Name</td>
        <td style="width: 30%;">Machine Name</td>
        <td style="width: 10%;">Machine Qty</td>
        <td style="width: 10%;">SMV</td>
        <td style="width: 10%;">Action</td>
    </tr>
    </thead>
    <tbody>
    <tr id='addr0' data-id="0" class="hidden">
        <td data-name="ProcessName">
            {{Form::text('ProcessName', '', ['id'=>'ProcessName', 'class' => 'form-control', 'placeholder'=>''])}}
        </td>
        <td data-name="MachineName">
            <div class="form-group row-fluid m-auto">
                <select name="MachineName" class="form-control" id="MachineName" data-live-search="true">
                    <option value=""></option>
                    @foreach($machineName as $machineName)
                    <option value="{{$machineName->id}}">{{$machineName->MachineName}}</option>
                    @endforeach
                </select>
            </div>
        </td>
        <td data-name="MachineQty">
            {{Form::number('MachineQty', '', ['id'=>'MachineQty', 'class' => 'form-control', 'placeholder'=>''])}}
        </td>
        <td data-name="SMV">
            {{Form::number('SMV', '', ['id'=>'SMV', 'class' => 'form-control', 'placeholder'=>''])}}
        </td>
        <td data-name="del">
            <a name="del0" id="del0" class="btn btn-outline-danger row-remove"  value="del0">Delete</a>
            {{-- <button name="del0" class='btn btn-outline-danger row-remove'>Delete</button> --}}
        </td>
    </tr>
    </tbody>

    <tfoot>
    <tr>
        <td colspan="5" style="text-align: left;">
            <a id="add_row" class="btn float-right btn-lg btn-block btn-outline-secondary">Add Row</a>
        </td>
    </tr>
    <tr>
        <td colspan="5" style="text-align: left;">
            {{Form::submit('Save', ['class'=>'btn btn-lg btn-outline-primary btn-block', 'name'=>'ProductInsert'])}}
        </td>
    </tr>
    </tfoot>
</table>

这是我的控制器

// Create Post
$work_breakdown = new work_breakdown;
$work_breakdown->Buyer = $request->input('Buyer');
$work_breakdown->Style = $request->input('Style');
$work_breakdown->Item = $request->input('Item');
$work_breakdown->Size = $request->input('Size');
$work_breakdown->ThreadType = $request['Thread'];
$work_breakdown->Description = $request->input('Description');
$work_breakdown->ProductImage = $fileNameToStore;
$work_breakdown->user_id = auth()->user()->id;
$work_breakdown->save();

$maxValue = work_breakdown::max('id');
$i = 0;
$ProcessName_ID = $request->input('MachineName');
foreach($ProcessName_ID as $key => $MachineName) {

    $i++;
// Create Post
    $work_breakdown = new work_breakdown;
    $work_breakdown->ProductID = $maxValue;
    $work_breakdown->MachineID = $MachineName;
    $work_breakdown->ProcessName = $request->input('ProcessName');
    $work_breakdown->MachineQty = $request['MachineQty'];
    $work_breakdown->SMV = $request['SMV'];
    $work_breakdown->user_id = auth()->user()->id;
    $work_breakdown->save();
}

我发现这个错误:“为foreach()提供的参数无效”
在此处输入图像描述

avkwfej4

avkwfej41#

当我第一次开始将数组作为表单输入传递时,这是一个典型的棘手概念示例。这是这个问题的一个变体,但是您可以将其留空来创建一个0索引数组,而不是定义键。
表单中的每一行都需要一个“类似对象”的格式。在json中,您希望将此消息发送到服务器:

[
  {
    "ProcessName": "First process",
    "MachineID": "123",
    "MachineQty": "1",
    "SMV": "10"
  },
  {
    "ProcessName": "Second process",
    "MachineID": "456",
    "MachineQty": "2",
    "SMV": "20"
  }
]

要在html中实现这一点,您需要使用 name html输入的属性。

<tr id='addr0' data-id="0" class="hidden">
    <td data-name="ProcessName">
        {{Form::text('items[][ProcessName]', '', ['id'=>'ProcessName', 'class' => 'form-control', 'placeholder'=>''])}}
    </td>
    <td data-name="MachineName">
        <div class="form-group row-fluid m-auto">
            <select name="items[][MachineName]" 
                    class="form-control" 
                    id="MachineName" 
                    data-live-search="true">
                <option value=""></option>
                @foreach($machineName as $machineName)
                <option value="{{$machineName->id}}">{{$machineName->MachineName}}</option>
                @endforeach
            </select>
        </div>
    </td>
    <td data-name="MachineQty">
        {{Form::number('items[][MachineQty]', '', ['id'=>'MachineQty', 'class' => 'form-control', 'placeholder'=>''])}}
    </td>
    <td data-name="SMV">
        {{Form::number('items[][SMV]', '', ['id'=>'SMV', 'class' => 'form-control', 'placeholder'=>''])}}
    </td>
    <td data-name="del">
        <a name="del0" id="del0" class="btn btn-outline-danger row-remove"  value="del0">Delete</a>
    </td>
</tr>

在您的控制器中,您现在可以通过执行以下操作对每个项目进行循环:

//  ... initial setup here

foreach($request->input('items', []) as $item){
    $work_breakdown = new work_breakdown($item);
    // do other logic and then save to database
}

希望这有帮助!

sr4lhrrt

sr4lhrrt2#

我用一种巧妙的方法解决了这个问题。如果有人知道标准方法,请告诉我。在这里,我改变了我的看法页这样

<table class="table table-hover table-sortable" id="tab_logic" name="DataTable">
                                <thead>
                                    <tr class="text-center">
                                        <td style="width: 2%;" class="text-white">SN</td>
                                        <td style="width: 38%;">Process Name</td>
                                        <td style="width: 30%;">Machine Name</td>
                                        <td style="width: 10%;">Machine Qty</td>
                                        <td style="width: 10%;">SMV</td>
                                        <td style="width: 10%;">Action</td>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr id='addr0' data-id="0" class="d-none">
                                        <td data-name="SN" class="d-none">
                                            <div class="form-group row-fluid m-auto">
                                            <input name="items[][SN]" type="text" class="form-control d-none" id="forIncrementalRow">
                                            </div>
                                        </td>
                                        <td data-name="ProcessName">
                                            {{Form::text('ProcessName0', '', ['id'=>'ProcessName', 'class' => 'form-control', 'placeholder'=>''])}}
                                        </td>
                                        <td data-name="MachineName">
                                                <select name="MachineName0" class="form-control row-fluid m-auto" id="MachineName" data-live-search="true">
                                                    <option value=""></option>
                                                    @foreach($machineName as $machineName)
                                                        <option value="{{$machineName->id}}">{{$machineName->MachineName}}</option>
                                                    @endforeach
                                                </select>
                                        </td>
                                        <td data-name="MachineQty">
                                        {{Form::number('MachineQty0', '', ['id'=>'MachineQty', 'class' => 'form-control', 'placeholder'=>''])}}
                                        </td>
                                        <td data-name="SMV">
                                        {{Form::number('SMV0', '', ['id'=>'SMV', 'class' => 'form-control', 'placeholder'=>''])}}
                                        </td>
                                        <td data-name="del">
                                            <a name="del0" id="del0" class="btn btn-outline-danger row-remove"  value="del0">Delete</a>                        
                                            {{-- <button name="del0" class='btn btn-outline-danger row-remove'>Delete</button> --}}
                                        </td>
                                    </tr>
                                </tbody>

                                <tfoot>
                                    <tr>
                                        <td colspan="6" style="text-align: left;">
                                            <a id="add_row" class="btn float-right btn-lg btn-block btn-outline-secondary">Add Row</a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="6" style="text-align: left;">
                                            {{Form::submit('Save', ['class'=>'btn btn-lg btn-outline-primary btn-block', 'name'=>'ProductInsert'])}}
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>

还有控制器

$maxValue = product_details::max('id');
            $i=0;
            $j=0;
            foreach($request->input('items', []) as $item){

            $j++;
            // Create Post
            $work_breakdown = new work_breakdown($item);
            $work_breakdown->ProductID = $maxValue;
            $work_breakdown->SN = $j;
            $work_breakdown->ProcessName = $request->input("ProcessName{$j}");
            $work_breakdown->MachineID = $request->input("MachineName{$j}");
            $work_breakdown->MachineQty = $request["MachineQty{$j}"];
            $work_breakdown->SMV = $request["SMV{$j}"];
            $work_breakdown->user_id = auth()->user()->id;
            $work_breakdown->save(); 
            }

            $userID = auth()->user()->id;

            //to delete unexpected row
            $maxValue2 = work_breakdown::where('user_id', $userID)->max('id');
            DB::delete('delete from work_breakdown where id = ?',[$maxValue2]);

这个循环总是用空值保存一个额外的行,所以我在循环之后删除它

相关问题