Saturday, April 15, 2017

Set hasOne relation to new instance of child model

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();



via user3720435

Advertisement