@@ -52,75 +52,7 @@ public function crudDelete( $namespace, $id )
5252 throw new NotFoundException ( __ ( 'Unable to delete an entry that no longer exists. ' ) );
5353 }
5454
55- if ( method_exists ( $ model , 'getDeclaredDependencies ' ) ) {
56- /**
57- * Let's verify if the current model
58- * is a dependency for other models.
59- */
60- $ declaredDependencies = $ model ->getDeclaredDependencies ();
61-
62- foreach ( $ declaredDependencies as $ class => $ indexes ) {
63- $ localIndex = $ indexes [ 'local_index ' ] ?? 'id ' ;
64- $ request = $ class ::where ( $ indexes [ 'foreign_index ' ], $ model ->$ localIndex );
65- $ dependencyFound = $ request ->first ();
66- $ countDependency = $ request ->count () - 1 ;
67-
68- if ( $ dependencyFound instanceof $ class ) {
69- if ( isset ( $ model ->{ $ indexes [ 'local_name ' ] } ) && ! empty ( $ indexes [ 'foreign_name ' ] ) ) {
70- /**
71- * if the foreign name is an array
72- * we'll pull the first model set as linked
73- * to the item being deleted.
74- */
75- if ( is_array ( $ indexes [ 'foreign_name ' ] ) ) {
76- $ relatedSubModel = $ indexes [ 'foreign_name ' ][0 ]; // model name
77- $ localIndex = $ indexes [ 'foreign_name ' ][1 ]; // local index on the dependency table $dependencyFound
78- $ foreignIndex = $ indexes [ 'foreign_name ' ][2 ] ?? 'id ' ; // foreign index on the related table $model
79- $ labelColumn = $ indexes [ 'foreign_name ' ][3 ] ?? 'name ' ; // foreign index on the related table $model
80-
81- /**
82- * we'll find if we find the model
83- * for the provided details.
84- */
85- $ result = $ relatedSubModel ::where ( $ foreignIndex , $ dependencyFound ->$ localIndex )->first ();
86-
87- /**
88- * the model might exists. If that doesn't exists
89- * then probably it's not existing. There might be a misconfiguration
90- * on the relation.
91- */
92- if ( $ result instanceof $ relatedSubModel ) {
93- $ foreignName = $ result ->$ labelColumn ?? __ ( 'Unidentified Item ' );
94- } else {
95- $ foreignName = $ result ->$ labelColumn ?? __ ( 'Unexisting Item ' );
96- }
97- } else {
98- $ foreignName = $ dependencyFound ->{ $ indexes [ 'foreign_name ' ] } ?? __ ( 'Unidentified Item ' );
99- }
100-
101- /**
102- * The local name will always pull from
103- * the related model table.
104- */
105- $ localName = $ model ->{ $ indexes [ 'local_name ' ] };
106-
107- throw new NotAllowedException ( sprintf (
108- __ ( 'Unable to delete "%s" as it \'s a dependency for "%s"%s ' ),
109- $ localName ,
110- $ foreignName ,
111- $ countDependency >= 1 ? ' ' . trans_choice ( '{1} and :count more item.|[2,*] and :count more items. ' , $ countDependency , [ 'count ' => $ countDependency ] ) : '. '
112- ) );
113- } else {
114- throw new NotAllowedException ( sprintf (
115- $ countDependency === 1 ?
116- __ ( 'Unable to delete this resource as it has %s dependency with %s item. ' ) :
117- __ ( 'Unable to delete this resource as it has %s dependency with %s items. ' ),
118- $ class
119- ) );
120- }
121- }
122- }
123- }
55+ $ resource ->handleDependencyForDeletion ( $ model );
12456
12557 /**
12658 * Run the filter before deleting
0 commit comments