Erhnam
Erhnam

Reputation: 921

Laravel NeoEloquent - Dynamic relations between same node class

I'm working on a proof of concept using Laravel and Neo4j as a backend. NeoEloquent is the pefered choice for now: https://github.com/Vinelab/NeoEloquent

For the moment I have a 'person' model with a hasmany relation to 'term'. This works well, just as it is described in: https://github.com/Vinelab/NeoEloquent#one-to-many

The next step is to create dynamic relationships. So a term can have a relationship to another term. The relationship type also has to flexible. So it can be a kind of, copied, relation to, etc. Just like this:

enter image description here

The relationship types shouldn't be fixed and will be visualized at a later stage. What is the best approach for this? Can I do this with Polymorphic relations and HyperEdges? From what I understand is that with Polymorphic relations an additional node is created in between. This concept is different than how Neo4j works, where the edges have properties and attributes. Am I right? What is the best approach for this?

Upvotes: 0

Views: 498

Answers (1)

Erhnam
Erhnam

Reputation: 921

I sort of fixed it by using a Polymorphic relation. It's not optimal because an additional node is created with the properties I want to set on the relation, but it works for the moment. Here is the Relation Class I created:

class Relation extends NeoEloquent
{
    protected $label = 'Relation';
    protected $guarded = [];
    public $timestamps = false;
    protected $fillable = ['relation_name','relation_description'];
    public function subject()
    {
        return $this->morphMany('App\Term','TO');
    }
}

Here is the Term class:

class Term extends NeoEloquent
{
    protected $label = 'Term';
    protected $guarded = [];
    public $timestamps = false;
    protected $fillable = ['term_name','term_definition'];
    public function author()
    {
        return $this->belongsTo('App\User', 'CREATED');
    }
    public function relationship($morph=null)
    {
        return $this->hyperMorph($morph, 'App\Relation', 'RELATION', 'TO');
    }
    public function object()
    {
        return $this->belongsToMany('App\Term', 'RELATION');
    }
    public function whichRelation()
    {
        return $this->morphMany('App\Relation','TO');
    }
}

I open sourced some parts as a Laravel + NeoEloquent + Neo4j demo. The link to github is here: https://github.com/pietheinstrengholt/laravel-neo4j-neoeloquent-demo

Upvotes: 0

Related Questions