Vinelab/NeoEloquent - Realtionship to a exisiting node

426 Views Asked by At

I am using Vinelab\NeoEloquent for a new project for Neo4j. I have a registration where people can supply interests (as nodes). When registering, first the user is created, then the interests are created (if they don't exist), an given a relationship of "Interested_In" from the person to the interest nodes.

Problem I am having is when the interest already exists (unique by name property) and I just want to create a relationship from the person created and the Interest node.

In my People Model I have (Since 1 person can have many interests):

  public function interest(){
        return $this->hasMany('App\Models\Interest','Interested_In');
    }

In my create user function in my repository I have a section where it searches the Interests and returns one if one exists. Problem is I don't know how to then create the realtionship from People to Interest. I try to save it as

 $interests = explode(',',$input['tags']);

        $int = new NeoInterestRepository();
        foreach($interests as $interest){
            $existing = $int->find($interest);
            if(count($existing)){
                $person->interest()->save($existing);
            }else{
                $my_interest = $int->create($interest);
                $person->interest()->save($my_interest);
            }

        }

But I get 'Argument 1 passed to Vinelab\NeoEloquent\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, instance of Illuminate\Database\Eloquent\Collection given'

I see the associate() function but its only for BelongsTo which doesn't seem to apply.

Any ideas?

0

There are 0 best solutions below