0

Using Laravel 4.2, how can I set a hasOne relation on a model to an instance of a new model without touching the database?

I want to do the following, but Laravel is treating the instance as a property, not a child relation.

class ParentClass extends \Eloquent {
  public function child() {
    return $this-hasOne('ChildClass', 'child_id', 'parent_id');
  }
}

$parent = new ParentClass();
$parent->child = new ChildClass();
user3720435
  • 1,421
  • 1
  • 17
  • 27
  • It's been a while since I've used laravel but could you do `$parent = new ParentClass(); $parent->child()->create();`? Actually, I don't have an installation to test this on right now but `$parent->child() = new ChildClass();` may also work. You may jutst be missing the parenthesis. – user2027202827 Apr 14 '17 at 17:57
  • Thanks for response. ->create() wants to save to database. ->child() throws the error 'Can't use method return value in write context'. – user3720435 Apr 14 '17 at 18:04
  • Ok, how about this: `$child = new ChildClass(); $parent->child()->attach($child->id);` – user2027202827 Apr 14 '17 at 18:20
  • ->attach is part of BelongsToMany and not hasOne so unfortunately that doesn't work. – user3720435 Apr 14 '17 at 19:55
  • Sorry, you're right. After a little more reading, it seems what you're trying to do may not exactly be supported by eloquent. You may be able to get close enough though depending on what you're trying to do. First, if you look at the docs slightly below this anchor (https://laravel.com/docs/5.4/eloquent-relationships#the-save-method), they describe the `associate` method which can be used to associate a child with a parent (though it sounds like you're trying to go the other way around). – user2027202827 Apr 15 '17 at 03:02
  • If you look at the api (https://laravel.com/api/5.1/Illuminate/Database/Eloquent/Relations/HasOne.html) it doesn't seem there is a similar function to call on the parent to associate a child. You may be interested in this conversation though (http://stackoverflow.com/questions/23951222/eloquent-relations-attach-but-dont-save-to-has-many) where they seem to have found that this can be accomplished for `HasMany` relationships by just using `add`. I doubt `HasOne` is a collection as is the case for `HasMany` (I can't find proof in the docs/api) so it probably won't work, but maybe worth a try. – user2027202827 Apr 15 '17 at 03:11
  • user2027202827, thanks for your responses and efforts. – user3720435 Apr 15 '17 at 16:30

1 Answers1

0

To get through my problem, I have created a Trait class that I added to my models that allows me to set a relation. Doing this does not automatically set my parent/child relation values so I have to be careful to do this manually before I save/push.

trait ModelSetRelationTrait
{
  public function setRelation($key, $model)
  {
    $this->relations[$key] = $model;
  }
}

class ParentClass extends \Eloquent {
  use ModelSetRelationTrait;
  public function child() {
    return $this-hasOne('ChildClass', 'child_id', 'parent_id');
  }
}
user3720435
  • 1,421
  • 1
  • 17
  • 27