33namespace Kalnoy \Nestedset ;
44
55use Exception ;
6+ use Illuminate \Database \Eloquent \Builder ;
67use Illuminate \Database \Eloquent \Relations \BelongsTo ;
78use Illuminate \Database \Eloquent \Relations \HasMany ;
9+ use Illuminate \Database \Eloquent \SoftDeletingScope ;
810use LogicException ;
911use Illuminate \Database \Eloquent \Model as Eloquent ;
1012use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
@@ -46,24 +48,6 @@ class Node extends Eloquent
4648 */
4749 const AFTER = 'after ' ;
4850
49- /**
50- * Whether model uses soft delete.
51- *
52- * @var bool
53- *
54- * @since 1.1
55- */
56- static protected $ _softDelete ;
57-
58- /**
59- * Whether the node is being deleted.
60- *
61- * @since 2.0
62- *
63- * @var bool
64- */
65- static protected $ deleting ;
66-
6751 /**
6852 * Pending operation.
6953 *
@@ -97,23 +81,9 @@ protected static function boot()
9781 {
9882 parent ::boot ();
9983
100- static ::$ _softDelete = static ::getIsSoftDelete ();
101-
10284 static ::signOnEvents ();
10385 }
10486
105- /**
106- * Get whether model uses soft delete.
107- *
108- * @return bool
109- */
110- protected static function getIsSoftDelete ()
111- {
112- $ instance = new static ;
113-
114- return method_exists ($ instance , 'withTrashed ' );
115- }
116-
11787 /**
11888 * Sign on model events.
11989 */
@@ -132,7 +102,7 @@ protected static function signOnEvents()
132102 $ model ->deleteDescendants ();
133103 });
134104
135- if (static ::$ _softDelete ) {
105+ if (static ::usesSoftDelete () ) {
136106 static ::restoring (function (Node $ model ) {
137107 static ::$ deletedAt = $ model ->{$ model ->getDeletedAtColumn ()};
138108 });
@@ -146,7 +116,7 @@ protected static function signOnEvents()
146116 /**
147117 * {@inheritdoc}
148118 *
149- * Saves a node in a transaction.
119+ * Saves a node in transaction.
150120 */
151121 public function save (array $ options = array ())
152122 {
@@ -158,7 +128,7 @@ public function save(array $options = array())
158128 /**
159129 * {@inheritdoc}
160130 *
161- * Delete a node in transaction if model is not soft deleting .
131+ * Delete a node in transaction.
162132 */
163133 public function delete ()
164134 {
@@ -208,6 +178,22 @@ protected function callPendingAction()
208178 $ this ->moved = call_user_func_array ([ $ this , $ method ], $ parameters );
209179 }
210180
181+ /**
182+ * @return bool
183+ */
184+ public static function usesSoftDelete ()
185+ {
186+ static $ softDelete ;
187+
188+ if (is_null ($ softDelete )) {
189+ $ instance = new static ;
190+
191+ return $ softDelete = method_exists ($ instance , 'withTrashed ' );
192+ }
193+
194+ return $ softDelete ;
195+ }
196+
211197 /**
212198 * Make a root node.
213199 */
@@ -685,24 +671,19 @@ protected function insertNode($position)
685671 */
686672 protected function deleteDescendants ()
687673 {
688- if (static ::$ deleting ) return ;
689-
690674 $ lft = $ this ->getLft ();
691675 $ rgt = $ this ->getRgt ();
692676
693- // Make sure that inner nodes are just deleted and don't touch the tree
694- // This makes sense in Laravel 4.2
695- static ::$ deleting = true ;
696-
677+ /** @var QueryBuilder $query */
697678 $ query = $ this ->newQuery ()->whereNodeBetween ([ $ lft + 1 , $ rgt ]);
698679
699- if ( static :: $ _softDelete && $ this -> forceDeleting ) {
700- $ query -> withTrashed ()-> forceDelete ();
701- } else {
702- $ query ->delete ( );
680+ // Remove soft deleting scope when user forced delete so that descendants
681+ // are also deleted physically
682+ if ( $ this -> usesSoftDelete () && $ this -> forceDeleting ) {
683+ $ query ->withoutGlobalScope (SoftDeletingScope::class );
703684 }
704685
705- static :: $ deleting = false ;
686+ $ query -> applyScopes ()-> delete () ;
706687
707688 if ($ this ->hardDeleting ()) {
708689 $ height = $ rgt - $ lft + 1 ;
@@ -726,6 +707,7 @@ protected function restoreDescendants($deletedAt)
726707 $ this ->newQuery ()
727708 ->whereNodeBetween ([ $ this ->getLft () + 1 , $ this ->getRgt () ])
728709 ->where ($ this ->getDeletedAtColumn (), '>= ' , $ deletedAt )
710+ ->applyScopes ()
729711 ->restore ();
730712 }
731713
@@ -748,7 +730,7 @@ public function newEloquentBuilder($query)
748730 */
749731 public function newServiceQuery ()
750732 {
751- return static :: $ _softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
733+ return $ this -> usesSoftDelete () ? $ this ->withTrashed () : $ this ->newQuery ();
752734 }
753735
754736 /**
@@ -779,9 +761,8 @@ public function newFromBuilder($attributes = array(), $connection = null)
779761 *
780762 * @param Node $parent
781763 */
782- public static function create (array $ attributes = array (),
783- Node $ parent = null
784- ) {
764+ public static function create (array $ attributes = [], Node $ parent = null )
765+ {
785766 $ children = array_pull ($ attributes , 'children ' );
786767
787768 $ instance = new static ($ attributes );
@@ -1146,7 +1127,7 @@ protected function getArrayableRelations()
11461127 */
11471128 protected function hardDeleting ()
11481129 {
1149- return ! static :: $ _softDelete or $ this ->forceDeleting ;
1130+ return ! $ this -> usesSoftDelete () || $ this ->forceDeleting ;
11501131 }
11511132
11521133 /**
0 commit comments