22
33namespace Kalnoy \Nestedset ;
44
5- use \Illuminate \Database \Eloquent \Collection as BaseCollection ;
5+ use Illuminate \Database \Eloquent \Collection as BaseCollection ;
6+ use Illuminate \Database \Eloquent \Model ;
67
78class Collection extends BaseCollection
89{
@@ -19,15 +20,15 @@ public function linkNodes()
1920
2021 $ groupedChildren = $ this ->groupBy ($ this ->first ()->getParentIdName ());
2122
22- /** @var Node $node */
23+ /** @var NodeTrait|Model $node */
2324 foreach ($ this ->items as $ node ) {
2425 if ( ! isset ($ node ->parent )) {
2526 $ node ->setRelation ('parent ' , null );
2627 }
2728
2829 $ children = $ groupedChildren ->get ($ node ->getKey (), [ ]);
2930
30- /** @var Node $child */
31+ /** @var Model|NodeTrait $child */
3132 foreach ($ children as $ child ) {
3233 $ child ->setRelation ('parent ' , $ node );
3334 }
@@ -46,7 +47,7 @@ public function linkNodes()
4647 * If `$rootNodeId` is provided, the tree will contain only descendants
4748 * of the node with such primary key value.
4849 *
49- * @param int|Node |null $root
50+ * @param int|Model |null $root
5051 *
5152 * @return Collection
5253 */
@@ -59,7 +60,7 @@ public function toTree($root = null)
5960
6061 $ root = $ this ->getRootNodeId ($ root );
6162
62- /** @var Node $node */
63+ /** @var Model|NodeTrait $node */
6364 foreach ($ this ->items as $ node ) {
6465 if ($ node ->getParentId () == $ root ) $ items [] = $ node ;
6566 }
@@ -75,14 +76,16 @@ public function toTree($root = null)
7576 */
7677 protected function getRootNodeId ($ root = null )
7778 {
78- if ($ root instanceof Node) return $ root ->getKey ();
79+ if (NodeTrait::hasTrait ($ root )) {
80+ return $ root ->getKey ();
81+ }
7982
8083 // If root node is not specified we take parent id of node with
8184 // least lft value as root node id.
8285 if ($ root === null ) {
8386 $ leastValue = null ;
8487
85- /** @var Node $node */
88+ /** @var Model|NodeTrait $node */
8689 foreach ($ this ->items as $ node ) {
8790 if ($ leastValue === null || $ node ->getLft () < $ leastValue ) {
8891 $ leastValue = $ node ->getLft ();
0 commit comments