【发布时间】:2017-05-17 17:41:34
【问题描述】:
当我尝试使用 voyager 的 BREAD 创建器添加具有多对多关系的新项目时出现此错误:
SQLSTATE[23000]:违反完整性约束:1052 列 'id' 在 字段列表不明确(SQL:select
idfromproductsinner joinorder_productonproducts.id=order_product.product_id其中order_product.order_id为空)(查看: D:\WindowsFolders\Documents\PHP\Voyager-test\vendor\tcg\voyager\resources\views\bread\edit-add.blade.php)
我已按照文档进行操作,但不确定问题出在哪里。对我来说,我的多对多设置看起来应该在普通的 Laravel 中工作,但是当我尝试在 voyager 面板中添加一个新的 order 项目时它会抛出该错误。而且文档确实没有指定父表中的额外字段应该是什么(我在其中发表评论)。
这是我的order 和order-pivot 表;
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->text('comment')->nullable();
$table->integer('price');
$table->boolean('sent');
$table->boolean('paid');
$table->string('products'); // This thing
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->integer('amount');
$table->timestamps();
});
}
这是产品表:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->text('description');
$table->string('image');
$table->integer('price');
$table->string('slug')->unique();
$table->timestamps();
});
}
这里是关系函数:
public function products()
{
return $this->belongsToMany(Product::class, 'order_product');
}
最后一张来自Order BREAD 部分的图片:
【问题讨论】: