2017-08-21 71 views
0

模型1:如何添加引用laravel中另一個表的具有外鍵值的表?

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

模型2:

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

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

控制器:

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

[遷移>產品描述:

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

    }); 
} 

這是我的第一個表(模型)的遷移。它的主鍵爲'productdescriptionid'。

[遷移> 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']); 
    }); 
} 

在這裏我也提到了productdescriptionid外國key.And當我添加此salespackage表,值應該得到添加的productdescriptionid(productDescription)值。

但我得到的錯誤是無法添加或更新子行。

回答

1

你應該試試這個:

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

希望這對你的工作!

相關問題