im trying to make warehouse(very simple of course). I have 3 tables as it is:
Stores table
class CreateStoresTable extends Migration
{
public function up()
{
Schema::create('stores', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('user_id');
$table->timestamps();
});
}
Groups table:
public function up()
{
Schema::create('groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('visible');
$table->timestamps();
});
}
And a pivot table group_store:
public function up()
{
Schema::create('group_store', function(Blueprint $table) {
$table->integer('group_id')->unsigned()->index();
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
$table->integer('store_id')->unsigned()->index();
$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
});
}
Store Model -:
class Store extends Model
{
protected $table='stores';
protected $fillable = array('name','user_id');
public function group(){
return $this->hasMany('App\Group');
}
}
Group Model
class Group extends Model
{
protected $table='groups';
protected $fillable = array('name','visible');
public function store(){
return $this->belongsToMany('App\Store');
}
}
when i use this in tinker
$group=App\Group::find(4)
$group->store()->get() // and is working as it should
but when i try the reverse
$store=App\Store::first()
$store->group()->get() //i get this error message
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'groups.store_id' in 'where clause' (SQL: select * from groups
where groups
.store_id
= 3 and groups
.store_id
is not null)'
Iam trying to understand why eloquent is searching for store_id in groups table...