laravel - Set hasOne relation to new instance of child model -
using laravel 4.2, how can set hasone relation on model instance of new model without touching database?
i want following, laravel treating instance property, not child relation.
class parentclass extends \eloquent { public function child() { return $this-hasone('childclass', 'child_id', 'parent_id'); } } $parent = new parentclass(); $parent->child = new childclass();
to through problem, have created trait class added models allows me set relation. doing not automatically set parent/child relation values have careful manually before 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'); } }
Comments
Post a Comment