5
5
use Illuminate \Database \Eloquent \Model as Eloquent ;
6
6
use Franzose \ClosureTable \Contracts \EntityInterface ;
7
7
use Franzose \ClosureTable \Extensions \Collection ;
8
+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
8
9
use Illuminate \Database \Eloquent \Relations \HasMany ;
9
10
use Illuminate \Support \Arr ;
10
11
use InvalidArgumentException ;
@@ -166,7 +167,7 @@ public function setParentIdAttribute($value)
166
167
}
167
168
168
169
$ parentId = $ this ->getParentIdColumn ();
169
- $ this ->previousParentId = isset ( $ this ->original [$ parentId ]) ? $ this -> original [ $ parentId ] : null ;
170
+ $ this ->previousParentId = $ this ->original [$ parentId ] ?? null ;
170
171
$ this ->attributes [$ parentId ] = $ value ;
171
172
}
172
173
@@ -212,7 +213,7 @@ public function setPositionAttribute($value)
212
213
}
213
214
214
215
$ position = $ this ->getPositionColumn ();
215
- $ this ->previousPosition = isset ( $ this ->original [$ position ]) ? $ this -> original [ $ position ] : null ;
216
+ $ this ->previousPosition = $ this ->original [$ position ] ?? null ;
216
217
$ this ->attributes [$ position ] = max (0 , (int ) $ value );
217
218
}
218
219
@@ -298,7 +299,7 @@ public static function boot()
298
299
$ entity ->previousPosition = null ;
299
300
300
301
$ descendant = $ entity ->getKey ();
301
- $ ancestor = isset ( $ entity ->parent_id ) ? $ entity -> parent_id : $ descendant ;
302
+ $ ancestor = $ entity ->parent_id ?? $ descendant ;
302
303
303
304
$ entity ->closure ->insertNode ($ ancestor , $ descendant );
304
305
});
@@ -354,6 +355,16 @@ public function getParent(array $columns = ['*'])
354
355
return $ this ->exists ? $ this ->find ($ this ->parent_id , $ columns ) : null ;
355
356
}
356
357
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
+
357
368
/**
358
369
* Returns query builder for ancestors.
359
370
*
@@ -883,7 +894,7 @@ public function getChildrenRange($from, $to = null, array $columns = ['*'])
883
894
public function addChild (EntityInterface $ child , $ position = null , $ returnChild = false )
884
895
{
885
896
if ($ this ->exists ) {
886
- $ position = $ position !== null ? $ position : $ this ->getLatestChildPosition ();
897
+ $ position = $ position ?? $ this ->getLatestChildPosition ();
887
898
888
899
$ child ->moveTo ($ position , $ this );
889
900
}
@@ -1596,7 +1607,7 @@ private function buildSiblingQuery(Builder $builder, $id, callable $positionCall
1596
1607
public function addSibling (EntityInterface $ sibling , $ position = null , $ returnSibling = false )
1597
1608
{
1598
1609
if ($ this ->exists ) {
1599
- $ position = $ position === null ? static ::getLatestPosition ($ this ) : $ position ;
1610
+ $ position = $ position ?? static ::getLatestPosition ($ this );
1600
1611
1601
1612
$ sibling ->moveTo ($ position , $ this ->parent_id );
1602
1613
@@ -1623,7 +1634,7 @@ public function addSiblings(array $siblings, $from = null)
1623
1634
return $ this ;
1624
1635
}
1625
1636
1626
- $ from = $ from === null ? static ::getLatestPosition ($ this ) : $ from ;
1637
+ $ from = $ from ?? static ::getLatestPosition ($ this );
1627
1638
1628
1639
$ this ->transactional (function () use ($ siblings , &$ from ) {
1629
1640
foreach ($ siblings as $ sibling ) {
@@ -1876,7 +1887,7 @@ public function deleteSubtree($withSelf = false, $forceDelete = false)
1876
1887
* @param array $models
1877
1888
* @return \Illuminate\Database\Eloquent\Collection
1878
1889
*/
1879
- public function newCollection (array $ models = array () )
1890
+ public function newCollection (array $ models = [] )
1880
1891
{
1881
1892
return new Collection ($ models );
1882
1893
}
0 commit comments