Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel向数据库插入多个输入表单_Php_Jquery_Laravel - Fatal编程技术网

Php Laravel向数据库插入多个输入表单

Php Laravel向数据库插入多个输入表单,php,jquery,laravel,Php,Jquery,Laravel,我已经创建了一个多输入表单,如下图所示,我想保存到数据库中,问题是当我使用dd()时,它只显示第二行数据 如何将每一行保存到数据库 这是我的拉威尔控制器 public function store(Request $request) { $input = Input::all(); $condition = $input['service_id']; foreach ($condition as $key => $condition) {

我已经创建了一个多输入表单,如下图所示,我想保存到数据库中,问题是当我使用
dd()
时,它只显示第二行数据

如何将每一行保存到数据库

这是我的拉威尔控制器

public function store(Request $request)
{

        $input = Input::all();
        $condition = $input['service_id'];
        foreach ($condition as $key => $condition) {
            $detailorder = new DetailOrder;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            //$detailorder->mandays = $input['mandays'][$key];
            $detailorder->note = $input['note'][$key];
            }
            dd($detailorder);

}
这里是我的表脚本,我使用jQuery创建它

function getCorporateService(id){
    // get data and parsing to column
    $.get("{{ url('salesorder/service')}}/"+id, function(data){
        console.log(id);
        console.log(data);

        $.each(data, function (index, element){
            $br = "<tr id='item'>";
            $br += "<td> <input class='input-small' type='text' id='order_identifier' name='order_identifier[]' readonly></td>";
            $br += "<td><input class='input-small' type='text' id='service_name["+id+"]' name='service_name[]' value='"+element.service_name+"' readonly>"
                        +"<input class='input-small' type='hidden' id='service_id["+id+"]' name='service_id[]' value='"+element.id+"' readonly></td>";
            $br += "<td><select id='order_type["+id+"]' name='order_type[]'> <option> - </option> <option value='add'>Add</option> <option value='change'>Change</option> <option value='cancel'>Cancel</option> </select></td>";
            $br += "<td><input class='input-small' type='text' id='select_plan["+id+"]' name='select_plan[]'></td>";
            $br += "<td><input class='input-mini' type='text' id='qty["+id+"]' name='qty[]' value='1' onChange='getTotalPrice("+id+")'></td>";
            $br += "<td><input class='input-small' type='text' id='unit_price["+id+"]' name='unit_price[]' onChange='getTotalPrice("+id+")'></td>";
            $br += "<td><input class='input-small' type='text' id='total_price["+id+"]' name='total_price[]' onChange='getTotalPrice("+id+")'></td>";
            $br += "<td><textarea class='input-small' id='notes["+id+"]' name='note[]'></textarea></td>";
            $br += "</tr>";

            $(".corporatesvc").append($br);

        });
   });
}
函数getCorporateService(id){ //获取数据并解析到列 $.get(“{{url('salesforder/service')}}/”+id,函数(数据){ console.log(id); 控制台日志(数据); $.each(数据、函数(索引、元素){ $br=”“; $br+=”; $br+=“” +""; $br+=“-添加更改取消”; $br+=”; $br+=”; $br+=”; $br+=”; $br+=”; $br+=”; $(“.corporatesvc”)。追加($br); }); }); }
检查以下行:

foreach ($condition as $key => $condition) {
    $detailorder = new DetailOrder;
    // here you are creating a new object on each iteration, means on each iteration new object overrides old one, that's why you are getting the last record only
}

要解决此问题,请将此对象创建代码置于循环之外,然后重试。

检查以下行:

foreach ($condition as $key => $condition) {
    $detailorder = new DetailOrder;
    // here you are creating a new object on each iteration, means on each iteration new object overrides old one, that's why you are getting the last record only
}

要解决此问题,请将此对象创建代码置于循环之外,然后重试。

我不知道您的
dd
函数。当前循环运行并消失所有旧命令,并在
dd函数中传递最后一个命令,因此您需要创建一个对象数组并在函数中传递它,如

$allOrders=array(); // make an array, for storing all detail order in it
foreach ($condition as $key => $condition) {
    $detailorder = new DetailOrder;

    //you can use to ignore $key::=> $detailorder->serivce_id = $condition['service_id'];

    $detailorder->serivce_id = $input['service_id'][$key];
    $detailorder->order_type = $input['order_type'][$key];
    $detailorder->select_plan = $input['select_plan'][$key];
    $detailorder->qty = $input['qty'][$key];
    $detailorder->unit_price = $input['unit_price'][$key];
    //$detailorder->mandays = $input['mandays'][$key];
    $detailorder->note = $input['note'][$key];
    $allOrders[]=$detailorder;
}
dd($allOrders); // pass it to the function

我不知道你的
dd
功能。当前循环运行并消失所有旧命令,并在
dd函数中传递最后一个命令,因此您需要创建一个对象数组并在函数中传递它,如

$allOrders=array(); // make an array, for storing all detail order in it
foreach ($condition as $key => $condition) {
    $detailorder = new DetailOrder;

    //you can use to ignore $key::=> $detailorder->serivce_id = $condition['service_id'];

    $detailorder->serivce_id = $input['service_id'][$key];
    $detailorder->order_type = $input['order_type'][$key];
    $detailorder->select_plan = $input['select_plan'][$key];
    $detailorder->qty = $input['qty'][$key];
    $detailorder->unit_price = $input['unit_price'][$key];
    //$detailorder->mandays = $input['mandays'][$key];
    $detailorder->note = $input['note'][$key];
    $allOrders[]=$detailorder;
}
dd($allOrders); // pass it to the function

您可以使用以下代码插入多个项目:

DB::table('tablename')->insert($tableitems);

您可以使用以下代码插入多个项目:

DB::table('tablename')->insert($tableitems);

公共函数存储(Request$Request)
中添加行,如下所示

public function store(Request $request)
{

        $input = Input::all();
        $condition = $input['service_id'];
        foreach ($condition as $key => $condition) {
            $detailorder = new DetailOrder;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            //$detailorder->mandays = $input['mandays'][$key];
            $detailorder->note = $input['note'][$key];

            //for saving each DetailOrder to database
            $detailorder->save();
            } 
}
注意这里的
save()
应该从
foreach
循环中调用,以将每一行保存到数据库中


考虑到JavaScript工作正常,上述修改将保存数据库中每一行的数据。

公共函数存储(Request$Request)
中添加行,如下所示

public function store(Request $request)
{

        $input = Input::all();
        $condition = $input['service_id'];
        foreach ($condition as $key => $condition) {
            $detailorder = new DetailOrder;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            //$detailorder->mandays = $input['mandays'][$key];
            $detailorder->note = $input['note'][$key];

            //for saving each DetailOrder to database
            $detailorder->save();
            } 
}
注意这里的
save()
应该从
foreach
循环中调用,以将每一行保存到数据库中

考虑到JavaScript工作正常,上述修改将保存数据库中每一行的数据