gpt4 book ai didi

mysql - Voyager 管理面板中的多对多关系

转载 作者:行者123 更新时间:2023-11-29 06:04:58 30 4
gpt4 key购买 nike

当我尝试使用 BREAD creator for voyager 添加具有多对多关系的新项目时出现此错误:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select id from products inner join order_product on products.id = order_product.product_id where order_product.order_id is null) (View: D:\WindowsFolders\Documents\PHP\Voyager-test\vendor\tcg\voyager\resources\views\bread\edit-add.blade.php)

我已按照文档进行操作,但不太确定问题出在哪里。在我看来,我的多对多设置应该可以在正常的 Laravel 中运行,但是当我尝试在航海者面板中添加新的 order 项目时,它会抛出该错误。而且文档确实没有指定父表中的额外字段应该是什么(我在哪里发表评论)。

这是我的orderorder-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 部分的图片:

enter image description here

最佳答案

在这种情况下,MySQL 错误消息非常清楚:productsorder_product 表都有 id 字段。题中sql语句的select列表只有id,MySQL无法决定id字段应该从哪个表中选择。

需要在字段名前加表名前缀,以明确:

select products.id from ... 

关于mysql - Voyager 管理面板中的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299469/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com