1

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?

Wally Kolcz
  • 1,604
  • 3
  • 24
  • 45
  • Apparently the repository's find() method is returning a collection while it is expected to return an instance instead. Please post the code inside `NeoInterestRepository::find` – mulkave Sep 07 '15 at 08:20
  • Return \App\Models\Interest::where('name',$name)->first(); – Wally Kolcz Sep 07 '15 at 15:49
  • Do I need to use Interest::find() but how do I find it by name instead of id? Do I first find the node by name, the use ::find() using the id from the result? – Wally Kolcz Sep 07 '15 at 15:50
  • This is very strange, calling `->first()` should return either a model instance or `null` but in your case it's a `Collection`. Can you please try `find()` by id once just for the sake of debugging? – mulkave Sep 08 '15 at 07:23
  • Did you tried `$person->interest()->attach($interest)` as you have `hasMany` relationship? – Indrasinh Bihola Jun 21 '16 at 04:00

0 Answers0