22
33namespace Kalnoy \Nestedset ;
44
5+ use Carbon \Carbon ;
56use Illuminate \Database \Eloquent \Builder ;
67use Illuminate \Database \Eloquent \Model ;
78use Illuminate \Database \Eloquent \ModelNotFoundException ;
@@ -77,10 +78,11 @@ public function whereIsRoot()
7778 * @since 2.0
7879 *
7980 * @param mixed $id
81+ * @param bool $andSelf
8082 *
8183 * @return $this
8284 */
83- public function whereAncestorOf ($ id )
85+ public function whereAncestorOf ($ id, $ andSelf = false )
8486 {
8587 $ keyName = $ this ->model ->getKeyName ();
8688
@@ -109,11 +111,23 @@ public function whereAncestorOf($id)
109111 $ this ->query ->whereRaw ("{$ value } between {$ lft } and {$ rgt }" );
110112
111113 // Exclude the node
112- $ this ->where ($ keyName , '<> ' , $ id );
114+ if ( ! $ andSelf ) {
115+ $ this ->where ($ keyName , '<> ' , $ id );
116+ }
113117
114118 return $ this ;
115119 }
116120
121+ /**
122+ * @param $id
123+ *
124+ * @return QueryBuilder
125+ */
126+ public function whereAncestorOrSelf ($ id )
127+ {
128+ return $ this ->whereAncestorOf ($ id , true );
129+ }
130+
117131 /**
118132 * Get ancestors of specified node.
119133 *
@@ -129,6 +143,17 @@ public function ancestorsOf($id, array $columns = array( '*' ))
129143 return $ this ->whereAncestorOf ($ id )->get ($ columns );
130144 }
131145
146+ /**
147+ * @param $id
148+ * @param array $columns
149+ *
150+ * @return \Kalnoy\Nestedset\Collection
151+ */
152+ public function ancestorsAndSelf ($ id , array $ columns = [ '* ' ])
153+ {
154+ return $ this ->whereAncestorOf ($ id , true )->get ($ columns );
155+ }
156+
132157 /**
133158 * Add node selection statement between specified range.
134159 *
@@ -329,6 +354,26 @@ public function whereIsBefore($id, $boolean = 'and')
329354 return $ this ->whereIsBeforeOrAfter ($ id , '< ' , $ boolean );
330355 }
331356
357+ /**
358+ * @return $this
359+ */
360+ public function whereIsLeaf ()
361+ {
362+ list ($ lft , $ rgt ) = $ this ->wrappedColumns ();
363+
364+ return $ this ->whereRaw ("$ lft = $ rgt - 1 " );
365+ }
366+
367+ /**
368+ * @param array $columns
369+ *
370+ * @return Collection
371+ */
372+ public function leaves (array $ columns = [ '* ' ])
373+ {
374+ return $ this ->whereIsLeaf ()->get ($ columns );
375+ }
376+
332377 /**
333378 * Include depth level into the result.
334379 *
@@ -877,20 +922,32 @@ protected static function reorderNodes(array &$dictionary, &$fixed,
877922 */
878923 public function rebuildTree (array $ data , $ delete = false )
879924 {
925+ if ($ this ->model ->usesSoftDelete ()) {
926+ $ this ->withTrashed ();
927+ }
928+
880929 $ existing = $ this ->get ()->getDictionary ();
881930 $ dictionary = [];
882931
883932 $ this ->buildRebuildDictionary ($ dictionary , $ data , $ existing );
884933
885934 if ( ! empty ($ existing )) {
886- if ($ delete ) {
935+ if ($ delete && ! $ this -> model -> usesSoftDelete () ) {
887936 $ this ->model
888937 ->newScopedQuery ()
889938 ->whereIn ($ this ->model ->getKeyName (), array_keys ($ existing ))
890- ->forceDelete ();
939+ ->delete ();
891940 } else {
892941 foreach ($ existing as $ model ) {
893942 $ dictionary [$ model ->getParentId ()][] = $ model ;
943+
944+ if ($ this ->model ->usesSoftDelete ()) {
945+ if ( ! $ model ->{$ model ->getDeletedAtColumn ()}) {
946+ $ time = $ this ->model ->fromDateTime ($ this ->model ->freshTimestamp ());
947+
948+ $ model ->{$ model ->getDeletedAtColumn ()} = $ time ;
949+ }
950+ }
894951 }
895952 }
896953 }
0 commit comments