I've 2 models User
and UserGroup
with ManyToMany relashionship
as the following:
UserGroup.php
:
public function users() {
return $this->belongsToMany('App\User', 'IN');
}
User.php
public function userGroup()
{
return $this->belongsToMany('App\UserGroup', 'IN');
}
I've added a user
in a usergroup
like this:
$u=User::find(myUserId)
to get user,and $g=UserGroup::find(myGroupId)
And $g->users()->attach($u)
and it works perfectly fine,and when i do $g->users()->get()
,it works also.But when i do $u->userGoup()->get()
it returns an empty array
.So the relashionship work on one side only usergroup->users
but not user->usergroup
Perhaps you need specify all parameters in 'belongsToMany' function