如何使用laravel将值数组插入db?

时间:2017-08-23 08:39:23

标签: javascript php mysql laravel

output of dd($formdescription->all());

var addInput3 = function(e) {
    var arr3 = [];
    $(".materialclass").each(function() {
        arr3.push($(this).val());
    });
    $('#materialvalue').html(arr3.join(","));
}

$(document).ready(function() {
   $(document).on("click", ".material", function(e) {
    e.preventDefault();
    var clone = '<div class="material1"><select class="materialclass"><option selected disabled>--Select One--</option><option value="No batteries">No batteries</option><option value="AA">AA</option><option value="AAA">AAA</option><option value="Lithium">Lithium</option></select><input type = "submit"    value = "-"    class = "remove" ><input type = "submit"    value = "+"    class = "material" ></div>';
    $(this).closest('.material1').after(clone);
  });
  $(document).on("click", ".remove", function(e) {
    e.preventDefault();
    $(this).parent(".material1").remove();
  });
    
});
$('.materialclass').unbind().bind('change', addInput3);

var addInput = function(e) {
    var arr = [];
    $("input.packageclass").each(function() {
        arr.push($(this).val());
    });
    //alert(arr);
    $('#salespackage').html(arr.join(","));
}

$(document).ready(function() {
    $(document).on("click", ".addnext", function(e) {
      e.preventDefault();
        var clone = '<div class="addpack"><input class="packageclass" type="text" name="package[]" id="package" placeholder="Ex.accessories"/><input type = "submit"    value = "-"    class = "remove" ><input type = "submit"    value = "+"    class = "addnext" ></div>';
        $(this).closest('.addpack').after(clone);
        $('.packageclass').unbind().bind('change', addInput);
    });
    $(document).on("click", ".remove", function(e) {
      e.preventDefault();
        $(this).parent(".addpack").remove();
    });

});
$('.packageclass').unbind().bind('change', addInput);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=container>
           <div class="addpack">
            <h6>Sales Package </h6>
            <div>
    <input type="text" name="package[]" placeholder="EX.accessories" id="package" class="packageclass">
            <input type="submit" value="+" class="addnext"></div>
            </div>
            </div>
            <br/>
            <br/>
            <div id="container">
            <div class="material1">
            <h6>Material </h6>
            <div>
            <select class="materialclass" name="material">
            <option selected disabled>--Select One--</option>
            <option value="No batteries">No batteries</option>
            <option value="AA">AA</option>
            <option value="AAA">AAA</option>
            <option value="Lithium">Lithium</option>
            </select>
             <input type="submit" value="+" class="material"></div>
             </div>
             </div>

这是我的laravel视图代码。在这里,当我点击添加按钮时,输入字段会重复,(显示在代码段中)。

我需要的是,为这些字段添加多个值。

例如,salespackage - &gt;配件         salespackage - &GT;手动

即,为同一字段添加多个值。as mentioned in the above eg.

但是,只有最后添加的值才会插入db。

控制器:

      public function addProductDetails(Request 
 $formdescription,$dataId,$pidvalue)
 {

         $description=new productDescription;
         $description->deviceCategoryId=$dataId;
         $description->product_id=$pidvalue;
         $description->descid=$this->getproductDescriptionId();
         $description->Width =$formdescription->input('width');
         $description->save();



 /* $salesPackage=new packageModel;
  $salesPackage->salesPackage=$formdescription->input('package');
  $salesPackage->productdesc()->associate($description->descid);
  $salesPackage->save();*/

 **$salesPackage = array();
     $salesPackage = $formdescription->input('package');
     //$id = $salesPackage->productdesc()->associate($description-
  >descid);**

     **$sales = new packageModel;
     $sales->productdesc()->associate($description->descid);
     foreach($salesPackage as $sp){

         //$sales->productdesc_id = $id;
         $sales->salesPackage   = $sp;
         $sales->save();

     }**
   }







 return response()->json([
    'modelName'    => $formdescription->mname,
    'colour' => $formdescription->colour,
    'rechargable' => $formdescription->rechargable,
    'batteryType' => $formdescription->batteryType
]);

 //$description->product()->associate($priceInfo);
}

以粗体标记的代码(控制器)用于添加数组值。 此外,我只尝试过salesPackage字段,但同样需要添加下拉值数组(即。,Material)。

1 个答案:

答案 0 :(得分:1)

希望这个简单的例子可以帮助你在db -

中插入值数组
            $money = new Money();

            $add = [
                'created_by'    => $request->user_id,
                'date'          => $request->date,
                'description'   => $request->description,
                'amount'        => $request->amount,
                'type'          => $request->type
            ];

            $res = $money->create($add);
            dd($res); // this will give you response of newly inserted record.