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

时间:2017-02-17 13:41:21

标签: mysql laravel

当我尝试使用BREAD创建者为voyager添加具有多对多关系的新项时,我收到此错误:

  

SQLSTATE [23000]:完整性约束违规:1052列'id'   字段列表不明确(SQL:从id内部联接中选择products   order_product上的productsid = order_productproduct_id   其中order_productorder_id为空)(查看:   d:\ WindowsFolders \文件\ PHP \旅行者测试\厂商\ TCG \旅行者\资源\视图\面包\编辑add.blade.php)

我已按照文档操作,并不确定我遇到问题的地方。对我来说,看起来我的多对多设置应该在正常的Laravel中工作,但是当我尝试在voyager面板中添加新的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

2 个答案:

答案 0 :(得分:1)

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

您需要在字段名称前加上表格名称,以便明确:

select products.id from ... 

答案 1 :(得分:0)

我遇到了类似的问题,并通过在Voyager Admin中的json选项中将关系键从id更改为product_id来解决此问题