@@ -104,7 +104,7 @@ public function sync($ids, $detaching = true)
104104
105105 $ records = $ this ->formatSyncList ($ ids );
106106
107- $ detach = array_diff ($ current , array_keys ($ records ));
107+ $ detach = array_values ( array_diff ($ current , array_keys ($ records) ));
108108
109109 // Next, we will take the differences of the currents and given IDs and detach
110110 // all of the entities that exist in the "current" array but are not in the
@@ -159,31 +159,28 @@ public function attach($id, array $attributes = array(), $touch = true)
159159 $ model = $ id ; $ id = $ model ->getKey ();
160160 }
161161
162- $ records = $ this ->createAttachRecords ((array ) $ id , $ attributes );
163-
164- // Get the ids to attach to the parent and related model.
165- $ otherIds = array_pluck ($ records , $ this ->otherKey );
166- $ foreignIds = array_pluck ($ records , $ this ->foreignKey );
162+ $ ids = (array ) $ id ;
167163
168164 // Attach the new ids to the parent model.
169- $ this ->parent ->push ($ this ->otherKey , $ otherIds , true );
165+ $ this ->parent ->push ($ this ->otherKey , $ ids , true );
170166
171- // If we have a model instance, we can psuh the ids to that model,
167+ // If we have a model instance, we can push the ids to that model,
172168 // so that the internal attributes are updated as well. Otherwise,
173169 // we will just perform a regular database query.
174170 if (isset ($ model ))
175171 {
176172 // Attach the new ids to the related model.
177- $ model ->push ($ this ->foreignKey , $ foreignIds , true );
173+ $ model ->push ($ this ->foreignKey , $ this -> parent -> getKey () , true );
178174 }
179175 else
180176 {
181177 $ query = $ this ->newRelatedQuery ();
182178
183- $ query ->where ($ this ->related ->getKeyName (), $ id );
179+ // Select related models.
180+ $ query ->whereIn ($ this ->related ->getKeyName (), $ ids );
184181
185- // Attach the new ids to the related model.
186- $ query ->push ($ this ->foreignKey , $ foreignIds , true );
182+ // Attach the new parent id to the related model.
183+ $ query ->push ($ this ->foreignKey , $ this -> parent -> getKey () , true );
187184 }
188185
189186 if ($ touch ) $ this ->touchIfTouching ();
0 commit comments