如何将两个表单作为参数传递给laravel控制器?

时间:2017-08-21 07:10:09

标签: php jquery mysql forms laravel-5.4

Laravel控制器代码:

public function addPriceDetails(Request $priceform,$dataId)
{
$priceInfo = new priceInfo ;
$priceInfo->deviceCategoryId=$dataId;
$priceInfo->productId=$this->getproductId();
$priceInfo->SKUID=$priceform->input('skuid');
$priceInfo->productName=$priceform->input('productName');
$priceInfo->listingStatus =$priceform->input('listingStatus');
$priceInfo->MRP =$priceform->input('mrp');
$priceInfo->sellingPrice=$priceform->input('selprice');
$priceInfo->fulfillmentBy =$priceform->input('fulfillment');
$priceInfo->procurementType =$priceform->input('procurementType');
$priceInfo->procurementSLA =$priceform->input('sla');
$priceInfo->stock =$priceform->input('stock');
$priceInfo->localDelCharge =$priceform->input('local');
$priceInfo->zonalDelCharge =$priceform->input('zonal');
$priceInfo->nationalDelCharge=$priceform->input('national');
$priceInfo->packWeight =$priceform->input('weight');
$priceInfo->packLength =$priceform->input('length');
$priceInfo->packBreadth =$priceform->input('breadth');
$priceInfo->packHeight =$priceform->input('height');
$priceInfo->HSN =$priceform->input('hsn');
$priceInfo->save();

return response()->json([
    'SKUID'    => $priceInfo->SKUID,
    'listingStatus' => $priceInfo->listingStatus,
    'MRP' => $priceInfo->MRP,
    'sellingPrice' => $priceInfo->sellingPrice
]);
}

这是我添加其中一个表单的值的函数。

第二种形式的控制器代码:

  public function addProductDetails(Request $formdescription,$dataId)
  {
   $description=new productDescription;
   $description->deviceCategoryId=$dataId;
   $description->productDescriptionId=$this-
   >getproductDescriptionId();
   $description->modelName=$formdescription->input('mname');
   $description->Height=$formdescription->input('height');
   $description->Weight=$formdescription->input('weight');
   $description->Depth=$formdescription->input('depth');
   $description->Width =$formdescription->input('width');
   $description->Type =$formdescription->input('type');
   $description->Character=$formdescription->input('character');
   $description->batteryType=$formdescription->input('batteryType');
   $description->salesPackage =$formdescription->input('package');
   $description->skillSet =$formdescription->input('skillSet');
   $description->Colour=$formdescription->input('colour');
   $description->Material =$formdescription->input('material');
   $description->maxAge=$formdescription->input('maxage');
   $description->minAge =$formdescription->input('minage');
   $description->batteryNos =$formdescription->input('batteryNos');
   $description->batteryOperated=$formdescription-
   >input('batteryOperated');
  $description->rechargable=$formdescription->input('rechargable');

   $description->save();



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

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

这是我添加其他表单值的另一个功能。但是我在这里使用product_Id值是一个外键值而且我需要在添加form之前得到它。我不知道如何得到它。 或者如果我有帮助将表单值传递给单个函数。

脚本代码:

    <script src = 
   "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
    </script>
  <script type="text/javascript">

   $(document).ready(function () {

   $("#priceSave").click(function(e){
    e.preventDefault();
    var form1 = $('#priceform').serialize();
   //alert(form1);
   $.ajax({
    url:'addPriceDetails/{{$dataId}}',
    type: "get",
    data: form1,
    dataType: 'json',
    success: function(response) {
      //alert(response.SKUID);
        $("#skuid").append(response.SKUID); 
        $("#mrp").append(response.MRP);       
        $("#lstatus").append(response.listingStatus); 
        $("#selprice").append(response.sellingPrice); 
      }
      });
      });



  $("#descSave").click(function(e){
   e.preventDefault();
   var form2 = $('#formdescription').serialize();
   alert(form2);
   $.ajax({
     url:'addProductDetails/{{$dataId}}',
    type: "get",
    data: form2,
    dataType: 'json',
    success: function(response) {
      //alert(response);
        $("#batterytype").append(response.batteryType); 
        $("#modelname").append(response.modelName);    
        $("#colour").append(response.colour); 
       // $("#colour").append(response.Colour); 
        $("#rechargable").append(response.rechargable); 
        //alert(response.Material);
        //alert(response.salesPackage);
      }
     });
     });


     });
     </script>

3 个答案:

答案 0 :(得分:1)

你应该试试这个:

return response()->json([
  'SKUID' => $priceInfo->SKUID,
  'listingStatus' => $priceInfo->listingStatus,
  'MRP' => $priceInfo->MRP,
  'sellingPrice' => $priceInfo->sellingPrice,
  'id' =>$this->getproductId()
]);

答案 1 :(得分:0)

将新产品详细信息插入数据库,您可以获取插入的产品ID。只需在保存$productID = $description->id;

后编写语句

答案 2 :(得分:0)

Laravel缓存您的对象及其值。我假设您已经定义了与产品详细信息和价格详细信息的关系。只需传递$ product-&gt; description() - &gt; save($ productInfo); 在$ product-&gt; pricing() - &gt; save($ description);

之后不久

为此,您必须在名为description()和pricing()的产品型号中具有两个函数,它们将定义它们与产品的关系。就像吃饭一样简单。另外定义一个fillables数组你正在做的事情很奇怪。