很多很多。在将额外的列保存到数据库时获取错误

时间:2018-04-25 17:49:55

标签: laravel laravel-5 laravel-4 many-to-many pivot

模型中的多对多关系

  public function drycleanings()
    {
      return $this->belongsToMany('App\drycleaning')
      ->withPivot('$cart_id','drycleaning_id','q_drycleaning_id');
    }

  public function carts()
  {
    return $this->belongsToMany('App\cart')
    ->withPivot('$cart_id','drycleaning_id','q_drycleaning_id');
  }

在我正在使用的控制器中

$cart->drycleanings()->attach($request->drycleaning_id,[$request->q_drycleaning_id]);

如果给我一个错误

SQLSTATE [42S22]:未找到列:1054未知列' 0'在'字段列表'

dd的结果($ request-> all());

"drycleaning_id" => array:2 [▼
    0 => "3"
    1 => "4"
  ]
  "q_drycleaning_id" => array:58 [▼
    0 => "2"
    1 => "2"
    2 => null
    3 => null
    4 => null
    5 => null
    6 => null
    7 => null
    8 => null
    9 => null
    10 => null
  ]

cart_drycleaning table的迁移

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCartDrycleaningTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cart_drycleaning', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('cart_id');
            $table->integer('drycleaning_id');
            $table->integer('q_drycleaning_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('cart_drycleaning');
    }
}

如果我只使用$cart->drycleanings()->attach($request->drycleaning_id); 然后将cart_id和drycleaning_id存储到关联/数据透视表中(表名:cart_drycleaning)

我正在尝试保存q_drycleaning_id(表示项目数量)作为用户的输入。

有什么办法我只能传递非空值吗?或任何其他解决方案,以保存到关联表?

0 个答案:

没有答案