@@ -104,7 +104,7 @@ public function sync($ids, $detaching = true)
104
104
105
105
$ records = $ this ->formatSyncList ($ ids );
106
106
107
- $ detach = array_diff ($ current , array_keys ($ records ));
107
+ $ detach = array_values ( array_diff ($ current , array_keys ($ records) ));
108
108
109
109
// Next, we will take the differences of the currents and given IDs and detach
110
110
// 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)
159
159
$ model = $ id ; $ id = $ model ->getKey ();
160
160
}
161
161
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 ;
167
163
168
164
// Attach the new ids to the parent model.
169
- $ this ->parent ->push ($ this ->otherKey , $ otherIds , true );
165
+ $ this ->parent ->push ($ this ->otherKey , $ ids , true );
170
166
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,
172
168
// so that the internal attributes are updated as well. Otherwise,
173
169
// we will just perform a regular database query.
174
170
if (isset ($ model ))
175
171
{
176
172
// Attach the new ids to the related model.
177
- $ model ->push ($ this ->foreignKey , $ foreignIds , true );
173
+ $ model ->push ($ this ->foreignKey , $ this -> parent -> getKey () , true );
178
174
}
179
175
else
180
176
{
181
177
$ query = $ this ->newRelatedQuery ();
182
178
183
- $ query ->where ($ this ->related ->getKeyName (), $ id );
179
+ // Select related models.
180
+ $ query ->whereIn ($ this ->related ->getKeyName (), $ ids );
184
181
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 );
187
184
}
188
185
189
186
if ($ touch ) $ this ->touchIfTouching ();
0 commit comments