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

时间:2022-09-27 13:40:14

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

输出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>

This is my laravel view code. Here,when I click on the add button,the input field gets duplicated,(shown in the snippet).

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

What I need here is, to add those multiple values for those fields.

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

For eg.,salespackage-->accessories salespackage-->manual

例如,salespackage - > accessories salespackage - > manual

ie., to add multiple values for the same field.如何使用laravel将值数组插入db?

即,为同一个字段添加多个值。

But,only the last added value get inserted into db.

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

Controller:

控制器:

      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);
}

The code(controller) marked in bold is used to add the array values. Also I have tried only for salesPackage field but,same is need for adding array of dropdown values also(ie., Material).

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

1 个解决方案

#1


1  

Hope this simple example will help you to insert array of values in db -

希望这个简单的例子可以帮助你在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.

#1


1  

Hope this simple example will help you to insert array of values in db -

希望这个简单的例子可以帮助你在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.