0

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

Christian LSANGOLA
  • 2,947
  • 4
  • 22
  • 36

2 Answers2

2

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}');
  • It's a nosql graph based database,so the following concept :`{COLUMN_NAME_ID_USERGROUP_ON_'IN'TABLE}', '{COLUMN_NAME_ID_USER_ON_'IN'TABLE}` doen't exist – Christian LSANGOLA Mar 29 '19 at 08:53
0

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

Christian LSANGOLA
  • 2,947
  • 4
  • 22
  • 36