1

Laravel controller code:

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

This is my function to add the values for one of the forms.

controller code for second form:

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

This is my another function for adding another form values. But here I am using product_Id value is a foreign key value and I need to get this with before added form.I don't know how to get this. Or else it is helpful if I pass both the form values in a single function.

Script Code:

    <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 Answers 3

0

You should try this:

return response()->json([
  'SKUID' => $priceInfo->SKUID,
  'listingStatus' => $priceInfo->listingStatus,
  'MRP' => $priceInfo->MRP,
  'sellingPrice' => $priceInfo->sellingPrice,
  'id' =>$this->getproductId()
]);
Sign up to request clarification or add additional context in comments.

Comments

0

Insert a new product details into database you can get the inserted product id. just write the statement after save $productID = $description->id;

1 Comment

hellooo..anyone?
0

Laravel Caches your objects and their values. I am assuming that you have defined the relations with the product details and price details. Just pass the $product->description()->save($productInfo); and soon after $product->pricing()->save($description);

for this you must have two functions in the product model named description() and pricing() they will define their relationship with the product. Its as easy as eating meal. Also define a fillables array what you are doing is much weird.

19 Comments

public function product(){ return $this->belongsTo(priceInfo::class); }
public function productDetails() { return $this->hasMany(priceInfo::class); }
These are the relationships defined between pricedetails and product description
Have you tried using these functions the way $product->relation-name()->save($the-related-variable) ???
No,I had some confusions with your variable namings
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.