nishanthi nishanthi - 1 month ago 16
MySQL Question

How to add table that has foreign key value referring to another table in laravel?

Model 1:

namespace App;
use Illuminate\Database\Eloquent\Model;

class productDescription extends Model
{
protected $table="ProductDescription";
protected $connection="mysql";

public function productPricing()
{
return $this->belongsTo(priceInfo::class);
}
public function salesPackage()
{
return $this->hasMany(packageModel::class);
}
}


Model2:

class packageModel extends Model
{
//
protected $table="subSalesPackage";
protected $connection="mysql";

public function product_description(){
return $this->belongsTo(productDescription::class);
}
}


Controller:

public function addProductDetails(Request $formdescription,$dataId)
{
$description=new productDescription;
$description->deviceCategoryId=$dataId;
$description->productdescriptionid=$this->getproductDescriptionId();
$description->modelName=$formdescription->input('mname');

$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');

//$product->productPricing()-save($priceInfo);
//$product->productDetails()->save($description);
$description->save();

$salesPackage=new packageModel;
$salesPackage->salesPackage=$formdescription->input('package');
**$salesPackage->product_description()->associate($description);**
$salesPackage->save();
//echo("success");

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

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


Migration->productdescription:

public function up()
{
//
Schema::create('ProductDescription', function (Blueprint $table) {
$table->engine='InnoDB';
$table->string('productdescriptionid')->primary();
$table->string('product_id');

$table->string('salesPackage');
$table->timestamps();
$table->index(['productDescriptionId']);

});
}


This is my migration for 1st table(model).It has the primary key as'productdescriptionid'.

Migration->subSalespackage

public function up()
{
//
Schema::create('subSalesPackage', function (Blueprint $table) {
$table->increments('id');
$table->string('product_description_id');
$table->string('salesPackage');
$table->foreign('product_description_id')-
>references('productdescriptionid')->on('ProductDescription');
$table->timestamps();
$table->index(['id']);
});
}


Here I have referred the productdescriptionid as foreign key.And when I add this salespackage table,the values should get added with the value of
productdescriptionid(productDescription)
.

But the error i'm getting is can't able to add or update a child row.

Answer Source

You should try this:

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

Hope this work for you !!!