@@ -14,12 +14,13 @@ class BelongsToMany extends EloquentBelongsToMany {
1414 */
1515 protected function hydratePivotRelation (array $ models )
1616 {
17- // Do nothing
17+ // Do nothing.
1818 }
1919
2020 /**
2121 * Set the select clause for the relation query.
2222 *
23+ * @param array $columns
2324 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
2425 */
2526 protected function getSelectColumns (array $ columns = array ('* ' ))
@@ -34,10 +35,21 @@ protected function getSelectColumns(array $columns = array('*'))
3435 */
3536 public function addConstraints ()
3637 {
37- if (static ::$ constraints )
38- {
39- $ this ->query ->where ($ this ->getForeignKey (), '= ' , $ this ->parent ->getKey ());
40- }
38+ if (static ::$ constraints ) $ this ->setWhere ();
39+ }
40+
41+ /**
42+ * Set the where clause for the relation query.
43+ *
44+ * @return $this
45+ */
46+ protected function setWhere ()
47+ {
48+ $ foreign = $ this ->getForeignKey ();
49+
50+ $ this ->query ->where ($ foreign , '= ' , $ this ->parent ->getKey ());
51+
52+ return $ this ;
4153 }
4254
4355 /**
@@ -82,7 +94,7 @@ public function create(array $attributes, array $joining = array(), $touch = tru
8294 /**
8395 * Sync the intermediate tables with a list of IDs or collection of models.
8496 *
85- * @param mixed $ids
97+ * @param array $ids
8698 * @param bool $detaching
8799 * @return array
88100 */
@@ -104,7 +116,11 @@ public function sync($ids, $detaching = true)
104116
105117 $ records = $ this ->formatSyncList ($ ids );
106118
107- $ detach = array_values (array_diff ($ current , array_keys ($ records )));
119+ $ detach = array_diff ($ current , array_keys ($ records ));
120+
121+ // We need to make sure we pass a clean array, so that it is not interpreted
122+ // as an associative array.
123+ $ detach = array_values ($ detach );
108124
109125 // Next, we will take the differences of the currents and given IDs and detach
110126 // all of the entities that exist in the "current" array but are not in the
@@ -113,7 +129,7 @@ public function sync($ids, $detaching = true)
113129 {
114130 $ this ->detach ($ detach );
115131
116- $ changes ['detached ' ] = (array ) array_map (' intval ' , $ detach );
132+ $ changes ['detached ' ] = (array ) array_map (function ( $ v ) { return ( int ) $ v ; } , $ detach );
117133 }
118134
119135 // Now we are finally ready to attach the new records. Note that we'll disable
@@ -156,33 +172,26 @@ public function attach($id, array $attributes = array(), $touch = true)
156172 {
157173 if ($ id instanceof Model)
158174 {
159- $ model = $ id ; $ id = $ model ->getKey ();
160- }
175+ $ model = $ id ;
161176
162- $ ids = ( array ) $ id ;
177+ $ id = $ model -> getKey () ;
163178
164- // Attach the new ids to the parent model.
165- $ this ->parent ->push ($ this ->otherKey , $ ids , true );
166-
167- // If we have a model instance, we can push the ids to that model,
168- // so that the internal attributes are updated as well. Otherwise,
169- // we will just perform a regular database query.
170- if (isset ($ model ))
171- {
172- // Attach the new ids to the related model.
179+ // Attach the new parent id to the related model.
173180 $ model ->push ($ this ->foreignKey , $ this ->parent ->getKey (), true );
174181 }
175182 else
176183 {
177184 $ query = $ this ->newRelatedQuery ();
178185
179- // Select related models.
180- $ query ->whereIn ($ this ->related ->getKeyName (), $ ids );
186+ $ query ->whereIn ($ this ->related ->getKeyName (), (array ) $ id );
181187
182188 // Attach the new parent id to the related model.
183189 $ query ->push ($ this ->foreignKey , $ this ->parent ->getKey (), true );
184190 }
185191
192+ // Attach the new ids to the parent model.
193+ $ this ->parent ->push ($ this ->otherKey , (array ) $ id , true );
194+
186195 if ($ touch ) $ this ->touchIfTouching ();
187196 }
188197
@@ -238,9 +247,9 @@ protected function buildDictionary(Collection $results)
238247
239248 foreach ($ results as $ result )
240249 {
241- foreach ($ result ->$ foreign as $ single )
250+ foreach ($ result ->$ foreign as $ item )
242251 {
243- $ dictionary [$ single ][] = $ result ;
252+ $ dictionary [$ item ][] = $ result ;
244253 }
245254 }
246255
0 commit comments