@@ -90,12 +90,15 @@ protected function relations($model, $foreignKeyIds)
9090 $ foreignKeyIdsUse = $ foreignKeyIds ;
9191
9292 //Many to many relations need to get related ids and related local key
93- if (get_class ($ modelRelation ) == 'Illuminate\Database\Eloquent\Relations\BelongsToMany ' ) {
94- extract ($ this ->gettBelongsToManyData ($ modelRelation , $ foreignKeyIds ));
93+ $ classModelRelation = get_class ($ modelRelation );
94+ if ($ classModelRelation == 'Illuminate\Database\Eloquent\Relations\BelongsToMany ' ) {
95+ extract ($ this ->getBelongsToManyData ($ modelRelation , $ foreignKeyIds ));
96+ } else if ($ classModelRelation == 'Illuminate\Database\Eloquent\Relations\MorphMany ' ) {
97+ extract ($ this ->getMorphManyData ($ modelRelation , $ foreignKeyIds ));
9598 }
9699
97100 $ affectedRowsOnExecute = $ this ->affectedRowsOnExecute ($ modelRelation , $ foreignKeyUse , $ foreignKeyIdsUse );
98-
101+
99102 if ($ action === 'restrict ' && $ affectedRowsOnExecute > 0 ) {
100103 DB ::rollBack (); //Rollback the transaction before throw exception
101104 throw (new SoftCascadeRestrictedException )->setModel (get_class ($ modelRelation ->getModel ()), $ foreignKeyUse , $ foreignKeyIdsUse ->toArray ());
@@ -112,7 +115,7 @@ protected function relations($model, $foreignKeyIds)
112115 * @param array $foreignKeyIds
113116 * @return array
114117 */
115- protected function gettBelongsToManyData ($ relation , $ foreignKeyIds )
118+ protected function getBelongsToManyData ($ relation , $ foreignKeyIds )
116119 {
117120 $ relationConnectionName = $ relation ->getConnection ()->getName ();
118121 $ relationTable = $ relation ->getTable ();
@@ -133,6 +136,30 @@ protected function gettBelongsToManyData($relation, $foreignKeyIds)
133136 ];
134137 }
135138
139+ /**
140+ * Get morph many related key ids and key use
141+ *
142+ * @param Illuminate\Database\Eloquent\Relations\Relation $relation
143+ * @param array $foreignKeyIds
144+ * @return array
145+ */
146+ protected function getMorphManyData ($ relation , $ foreignKeyIds )
147+ {
148+ $ relationConnectionName = $ relation ->getConnection ()->getName ();
149+ $ relatedClass = $ relation ->getRelated ();
150+ $ foreignKeyUse = $ relatedClass ->getKeyName ();
151+ $ foreignKeyIdsUse = $ relatedClass ::where ($ relation ->getMorphType (), $ relation ->getMorphClass ())
152+ ->whereIn ($ relation ->getQualifiedForeignKeyName (), $ foreignKeyIds )
153+ ->select ($ foreignKeyUse )
154+ ->get ()->toArray ();
155+ $ foreignKeyIdsUse = array_column ($ foreignKeyIdsUse , $ foreignKeyUse );
156+
157+ return [
158+ 'foreignKeyIdsUse ' => collect ($ foreignKeyIdsUse ),
159+ 'foreignKeyUse ' => $ relation ->getRelated ()->getKeyName ()
160+ ];
161+ }
162+
136163 /**
137164 * Execute delete, or restore.
138165 *
0 commit comments