diff --git a/src/Models/Entity.php b/src/Models/Entity.php index f0abb02..9f4435a 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model as Eloquent; use Franzose\ClosureTable\Contracts\EntityInterface; use Franzose\ClosureTable\Extensions\Collection; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Arr; use InvalidArgumentException; @@ -354,6 +355,16 @@ public function getParent(array $columns = ['*']) return $this->exists ? $this->find($this->parent_id, $columns) : null; } + /** + * Returns many-to-one relationship to the direct ancestor. + * + * @return BelongsTo + */ + public function parent() + { + return $this->belongsTo(get_class($this), $this->getParentIdColumn()); + } + /** * Returns query builder for ancestors. *