voyager: Many to Many Relationship Problem

  • Laravel Version: 5.5
  • Voyager Version: 1.0
  • PHP Version: 7

Description:

I have a Many to Many relationship Store <-> Item. Trying to set up the relationship inside Store with Belongs to Many but when I try to edit the store, I get this error:

ErrorException (E_ERROR) SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select idfromitemsinner joinitem_storeonitems.id=item_store.item_idwhereitem_store.store_id= 51)

@marktopper

Steps To Reproduce:

class Item extends Model
{

   public function stores()
   {
      return $this->belongsToMany(Store::class);
   }

}
class Store extends Model
{

   protected $fillable = ['name', 'address'];

   public function items()
   {
      return $this->belongsToMany(Item::class);
   }

}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (2 by maintainers)

Most upvoted comments

This is precisely why we discourage commenting on old/closed issues. The solution you’re proposing was merged in and released already.

I bet you have id column in your pivot table (item_store) ? You should use another primary key, for example [item, store]