2
2
3
3
namespace Kalnoy \Nestedset ;
4
4
5
+ use Carbon \Carbon ;
5
6
use Illuminate \Database \Eloquent \Builder ;
6
7
use Illuminate \Database \Eloquent \Model ;
7
8
use Illuminate \Database \Eloquent \ModelNotFoundException ;
@@ -77,10 +78,11 @@ public function whereIsRoot()
77
78
* @since 2.0
78
79
*
79
80
* @param mixed $id
81
+ * @param bool $andSelf
80
82
*
81
83
* @return $this
82
84
*/
83
- public function whereAncestorOf ($ id )
85
+ public function whereAncestorOf ($ id, $ andSelf = false )
84
86
{
85
87
$ keyName = $ this ->model ->getKeyName ();
86
88
@@ -109,11 +111,23 @@ public function whereAncestorOf($id)
109
111
$ this ->query ->whereRaw ("{$ value } between {$ lft } and {$ rgt }" );
110
112
111
113
// Exclude the node
112
- $ this ->where ($ keyName , '<> ' , $ id );
114
+ if ( ! $ andSelf ) {
115
+ $ this ->where ($ keyName , '<> ' , $ id );
116
+ }
113
117
114
118
return $ this ;
115
119
}
116
120
121
+ /**
122
+ * @param $id
123
+ *
124
+ * @return QueryBuilder
125
+ */
126
+ public function whereAncestorOrSelf ($ id )
127
+ {
128
+ return $ this ->whereAncestorOf ($ id , true );
129
+ }
130
+
117
131
/**
118
132
* Get ancestors of specified node.
119
133
*
@@ -129,6 +143,17 @@ public function ancestorsOf($id, array $columns = array( '*' ))
129
143
return $ this ->whereAncestorOf ($ id )->get ($ columns );
130
144
}
131
145
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
+
132
157
/**
133
158
* Add node selection statement between specified range.
134
159
*
@@ -329,6 +354,26 @@ public function whereIsBefore($id, $boolean = 'and')
329
354
return $ this ->whereIsBeforeOrAfter ($ id , '< ' , $ boolean );
330
355
}
331
356
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
+
332
377
/**
333
378
* Include depth level into the result.
334
379
*
@@ -877,20 +922,32 @@ protected static function reorderNodes(array &$dictionary, &$fixed,
877
922
*/
878
923
public function rebuildTree (array $ data , $ delete = false )
879
924
{
925
+ if ($ this ->model ->usesSoftDelete ()) {
926
+ $ this ->withTrashed ();
927
+ }
928
+
880
929
$ existing = $ this ->get ()->getDictionary ();
881
930
$ dictionary = [];
882
931
883
932
$ this ->buildRebuildDictionary ($ dictionary , $ data , $ existing );
884
933
885
934
if ( ! empty ($ existing )) {
886
- if ($ delete ) {
935
+ if ($ delete && ! $ this -> model -> usesSoftDelete () ) {
887
936
$ this ->model
888
937
->newScopedQuery ()
889
938
->whereIn ($ this ->model ->getKeyName (), array_keys ($ existing ))
890
- ->forceDelete ();
939
+ ->delete ();
891
940
} else {
892
941
foreach ($ existing as $ model ) {
893
942
$ 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
+ }
894
951
}
895
952
}
896
953
}
0 commit comments