Reputation: 567
I get this error when I try to add a new item with the many to many relationship using the BREAD creator for voyager:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select
id
fromproducts
inner joinorder_product
onproducts
.id
=order_product
.product_id
whereorder_product
.order_id
is null) (View: D:\WindowsFolders\Documents\PHP\Voyager-test\vendor\tcg\voyager\resources\views\bread\edit-add.blade.php)
I have followed the documentation and not really sure where I am having the issue. To me it looks like my many to many setup should work in normal Laravel, but it throws that error when I try to add a new order
item in the voyager panel. And the documentation really don't specify what the extra field in the parent table should be(where I put a comment).
Here is my order
and order-pivot
table;
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();
});
}
Here is the product table:
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();
});
}
Here is the relationship function:
public function products()
{
return $this->belongsToMany(Product::class, 'order_product');
}
And lastly a picture from the Order
BREAD part:
Upvotes: 3
Views: 6870
Reputation: 11
I had a similar issue and solved it by changing the relationship key from id
to product_id
inside the json options in Voyager Admin
Upvotes: 1
Reputation: 34232
The MySQL error message is quite clear in this case: both products
and order_product
tables have id
fields. The select list of the sql statement in the question simply has id
, therefore MySQL cannot decide which table the id
field should be selected from.
You need to prefix the field name with the table name to make it clear:
select products.id from ...
Upvotes: 3