【问题标题】:Laravel insert data from one table to another table errorLaravel 将数据从一张表插入另一张表报错
【发布时间】:2019-03-09 05:16:50
【问题描述】:

试图从表一中获取product_id,然后将其插入表二,但返回错误

1048 列“product_id”不能为空(SQL:插入product_detailsproduct_idcategorybrandprovider_id)值(外设、Riders、1))

代码:

$product = Product::create([
     'product_name' => $request['product_name'],
     'quantity' => $request['quantity']
]); 

$product->save();
$product_id = $product->id;

$productDetails = ProductDetails::create([
    'product_id' => $product_id,
    'category' => $request['category'],
    'brand' => $request['brand'],
    'provider_id' => $request['provider_id']
]);

可填写productdetails型号

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];

数据库结构:(记起来了,前段时间我对数据库做了一些改动,就是这个错误出现的时候。)

产品:

Schema::create('products', function (Blueprint $table) {
            $table->increments('product_id');
            $table->string('product_name');
            $table->integer('quantity');

产品详情:

Schema::create('product_details', function (Blueprint $table) {
            $table->integer('product_id')->unsigned();
            $table->string('category',255);
            $table->string('brand',255);
            $table->integer('provider_id')->unsigned();
            $table->foreign('product_id')
            ->references('product_id')
            ->on('products')
            ->onDelete('cascade');
            $table->timestamps();
        });

已经解决了

这只是我的一个简单错误。我应该在'$product_id = $product->id' 上使用'product_id' 而不是'id'。向大家道歉。

【问题讨论】:

  • 这个dd($product_id);的输出是什么?
  • 并显示 ProductDetails 模型的 fillable 属性
  • @InzamamIdrees 输出产品的 id。
  • 你的id生成了吗?
  • product_id 是自动递增的,所以是的

标签: php laravel


【解决方案1】:

在插入ProductDetails 之前检查Product 是否已保存。还要检查ProductDetails 模型中的可填写字段是否添加了product_id

    $product = Product::create([
            'product_name' => $request['product_name'],
            'quantity' => $request['quantity']
        ]); 

    if($product) {
        $productDetails = ProductDetails::create([
            'product_id' => $product->id,
            'category' => $request['category'],
            'brand' => $request['brand'],
            'provider_id' => $request['provider_id']
        ]);
    }

更新您的 fillable 字段,如下所示:

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];

更新

如果你的主键是product_id,你应该调用$product->product_id

【讨论】:

  • 我刚刚检查过,它确实在表中插入了一个产品,但随后返回 ProductDetails 错误
  • 您是否检查了ProductDetails 模型中的可填写字段?
  • 是的,可填充的与您刚才建议的相同。但仍然返回相同的错误
  • 你确定Product模型有id吗?
  • 天哪,就这么一个错误,我让你们浪费了所有的时间。谢谢 Manzurul 并道歉。
【解决方案2】:

您必须像这样更改您的 fillable 属性:

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];

【讨论】:

  • 是的,我刚才粘贴了错误的可填充项,抱歉。还是一样的问题
  • 您必须将您的可填写内容更改为此流程.....检查这样的订单.....'product_id', 'category', 'brand', 'provider_id'
  • 更改了流程,但仍然返回相同的错误。
  • 显示产品的数据库结构和productdetails
  • 刚刚编辑以显示两者的数据库迁移。
【解决方案3】:

我认为您不需要使用save() 方法。在 create() 方法之后,您可以获取模型对象并直接从中获取 id,但请确保您在 db 中的 id 是 id 而不是 product_id

$product = Product::create([
            'product_name' => $request['product_name'],
            'quantity' => $request['quantity']
        ]); 

 if($product){
    $productDetails = ProductDetails::create([
        'product_id' => $product->id,
        'category' => $request['category'],
        'brand' => $request['brand'],
        'provider_id' => $request['provider_id']
    ]);
 }else{
    echo "Something went wrong";
 }

【讨论】:

    【解决方案4】:

    你不应该在运行 create 之后运行 save() 方法;它是多余的。

    您可以在 try 中运行一个 db 事务并通过 catch 来了解究竟出了什么问题。

    try {
        \DB::transaction(function () use($request) {
            $product = Product::create([
                'product_name' => $request['product_name'],
                'quantity' => $request['quantity']
            ]);
    
            $productDetails = ProductDetails::create([
                'product_id' => $product->id,
                'category' => $request['category'],
                'brand' => $request['brand'],
                'provider_id' => $request['provider_id']
            ]);
        });
    
        dd('successful');
    } catch (\Exception $e) {
        dd( $e->getMessage() );
    }
    

    【讨论】:

      【解决方案5】:

      您需要在模型上指定 fillableguarded 属性,因为所有 Eloquent 模型都默认防止批量分配。

      Fillable 你指定哪些字段可以在你的模型中批量赋值,所以在模型中你还需要添加 product_id:

      型号:

      protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];
      ////only the field names inside the array can be mass-assign
      

      自动假定您的主键列将是id。为了让它正常工作,你应该在你的模型product.php中设置你的主键:

      protected $primaryKey = 'product_id';
      

      【讨论】:

      • 是的,我刚才粘贴了错误的可填充项,抱歉。还是一样的问题
      • 是的,在模型中将其更改为表产品的 primarKey 设置为 'product_id'
      • @Nel 你应该在你的模型中设置你的主键protected $primaryKey = 'product_id';
      • 是的,我的产品表的主键已经设置为“product_id”
      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多