Reputation: 3317
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
Upvotes: 0
Views: 43
Reputation: 3317
I finally figured out the problem.The problem
comes from the neoeloquent ORM
.They wanted to build a nosql schemaless graph based ORM
but their way of thinking is based on sql that eloquent ORM is based on
.So i was supposed to explicity define the 2 way relashionship when adding users to group on both ways:
$g->users()->attach($u)
and then $u->userGroups()->attach($g)
.In eloquent
,by defining $g->users()->attach($u)
,i could get all users of a group and get the group of a given user.But in neoeloquent
it not the case.But in the neo4j dbms
everything works fine for the both way by creating a one way
.So the problem is neoeloquent
Upvotes: 0
Reputation: 41
Perhaps you need specify all parameters in 'belongsToMany' function
return $this->belongsToMany('App\User', 'IN', '{COLUMN_NAME_ID_USERGROUP_ON_'IN'TABLE}', '{COLUMN_NAME_ID_USER_ON_'IN'TABLE}');
return $this->belongsToMany('App\UserGroup', 'IN', '{COLUMN_NAME_ID_USER_ON_'IN'TABLE}', '{COLUMN_NAME_ID_USERGROUP_ON_'IN'TABLE}');
Upvotes: 2