From c4f53b95f2924c6d41e9d70b6c32a0e1bf240a94 Mon Sep 17 00:00:00 2001 From: Jan Iwanow Date: Mon, 5 Oct 2020 20:18:31 +0700 Subject: [PATCH] introduced the "parent" relationship --- src/Models/Entity.php | 11 +++++++++++ 1 file changed, 11 insertions(+) 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. *