如何在laravel中添加具有外键值的表引用另一个表?

时间:2017-08-21 12:36:26

标签: php mysql laravel-5.4

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

模型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称为外键。当我添加此salespackage表时,值应添加productdescriptionid(productDescription)的值。

但我得到的错误是无法添加或更新子行。

1 个答案:

答案 0 :(得分:1)

你应该试试这个:

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

希望这对你有用!!!