Skip to content

Commit c4f53b9

Browse files
committed
introduced the "parent" relationship
1 parent 66a7af5 commit c4f53b9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Models/Entity.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Model as Eloquent;
66
use Franzose\ClosureTable\Contracts\EntityInterface;
77
use Franzose\ClosureTable\Extensions\Collection;
8+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89
use Illuminate\Database\Eloquent\Relations\HasMany;
910
use Illuminate\Support\Arr;
1011
use InvalidArgumentException;
@@ -354,6 +355,16 @@ public function getParent(array $columns = ['*'])
354355
return $this->exists ? $this->find($this->parent_id, $columns) : null;
355356
}
356357

358+
/**
359+
* Returns many-to-one relationship to the direct ancestor.
360+
*
361+
* @return BelongsTo
362+
*/
363+
public function parent()
364+
{
365+
return $this->belongsTo(get_class($this), $this->getParentIdColumn());
366+
}
367+
357368
/**
358369
* Returns query builder for ancestors.
359370
*

0 commit comments

Comments
 (0)