php - 如何添加具有外键值的表引用 Laravel 中的另一个表?

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

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

迁移->产品描述:

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”。

迁移->子销售包

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引用为外键。当我添加此销售包表时,这些值应该与productdescriptionid(productDescription)的值一起添加。

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

最佳答案

你应该尝试这个:

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

关于php - 如何添加具有外键值的表引用 Laravel 中的另一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45797325/

相关文章:

php - 我如何在字符串行中输出我的相关错误

php - 如何在 Laravel 5.4 中获取多选值

mysql - Laravel 迁移错误 : Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

php - 无法使用 PHP 连接到我的数据库服务器

PHP 将选定的数量发布到表中

php - php和C++能否在同一台服务器上运行并在一个网站中进行通信?

mysql - MySQL中如何限制数字字符的输入位数?

mysql - phpmyAdmin 中的重复键错误

php - MySQL 不能可靠地从 mysql_insert_id() 返回主键

php - 验证规则 required_if 与其他条件 (Laravel 5.4)