Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
635 stars 200 forks source link

Why is Many-To-Many an Incoming and not Outgoing Relationship #145

Closed AzeemMichael closed 8 years ago

AzeemMichael commented 8 years ago

I noticed One-To-One and One-To-Many are both outgoing relationships. But, Many-To-Many is an Incoming relationship. Shouldn't it be an Outgoing too? if User A follows User B. Shouldn't the relation be from A to B. It's A follows B, not, B follows A.

Mulkave commented 8 years ago

It depends on whether you're using belongsToMany (incoming) or hasMany (outgoing).

User A follows user B means A hasMany followers, yet B is followed by A means followedBy is an incoming relationship towards B.

Hope this makes it clear to you.

AzeemMichael commented 8 years ago

@Mulkave Thanks, using hasMany() on User side worked. Following Eloquent docs, I assumed we use belongsToMany() on both sides of the relationship for ManyToMany. i.e.,:

class User extends NeoEloquent {
    public function roles() {
        $this->belongsToMany(Role::class, 'ROLE');
    }
}

class Role extends NeoEloquent {
    public function users() {
        $this->belongsToMany(User::class, 'ROLE');
    }
}