diff --git a/src/Features/HasRelationships.php b/src/Features/HasRelationships.php index 1b847ec..380e071 100644 --- a/src/Features/HasRelationships.php +++ b/src/Features/HasRelationships.php @@ -18,6 +18,20 @@ trait HasRelationships HasOne, HasMany; + /** + * Does this model have a relationship for a given field? + * + * @param string $attribute + * @return boolean + * @author Ronan Chilvers + */ + public function hasRelation($attribute) + { + $method = $this->getRelationMethod($attribute); + + return method_exists($this, $method); + } + /** * Get the relation for a given attribute * @@ -26,12 +40,25 @@ trait HasRelationships */ public function getRelation($attribute) { - $shortAttribute = static::unprefix($attribute); - $method = 'relate' . Str::pascal($shortAttribute); + $method = $this->getRelationMethod($attribute); if (method_exists($this, $method)) { return $this->$method(); } return null; } + + /** + * Create a relationship method for a given attribute name + * + * @param string + * @return string + * @author Ronan Chilvers + */ + protected function getRelationMethod($attribute) + { + $shortAttribute = static::unprefix($attribute); + + return 'relate' . Str::pascal($shortAttribute); + } } diff --git a/src/Model.php b/src/Model.php index 1cd0c0d..d9aacd7 100644 --- a/src/Model.php +++ b/src/Model.php @@ -200,7 +200,14 @@ protected function boot() */ public function __isset($attribute) { - return $this->hasAttribute($attribute); + if ($this->hasAttribute($attribute)) { + return true; + } + if ($this->hasRelation($attribute)) { + return true; + } + + return false; } /** @@ -275,6 +282,9 @@ public function serialize() public function unserialize($serialized) { $this->fromArray(unserialize($serialized)); + if ($this->useTimestamps()) { + $this->bootHasTimestamps(); + } $this->boot(); }