@@ -14,12 +14,13 @@ class BelongsToMany extends EloquentBelongsToMany {
14
14
*/
15
15
protected function hydratePivotRelation (array $ models )
16
16
{
17
- // Do nothing
17
+ // Do nothing.
18
18
}
19
19
20
20
/**
21
21
* Set the select clause for the relation query.
22
22
*
23
+ * @param array $columns
23
24
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
24
25
*/
25
26
protected function getSelectColumns (array $ columns = array ('* ' ))
@@ -34,10 +35,21 @@ protected function getSelectColumns(array $columns = array('*'))
34
35
*/
35
36
public function addConstraints ()
36
37
{
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 ;
41
53
}
42
54
43
55
/**
@@ -82,7 +94,7 @@ public function create(array $attributes, array $joining = array(), $touch = tru
82
94
/**
83
95
* Sync the intermediate tables with a list of IDs or collection of models.
84
96
*
85
- * @param mixed $ids
97
+ * @param array $ids
86
98
* @param bool $detaching
87
99
* @return array
88
100
*/
@@ -104,7 +116,11 @@ public function sync($ids, $detaching = true)
104
116
105
117
$ records = $ this ->formatSyncList ($ ids );
106
118
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 );
108
124
109
125
// Next, we will take the differences of the currents and given IDs and detach
110
126
// 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)
113
129
{
114
130
$ this ->detach ($ detach );
115
131
116
- $ changes ['detached ' ] = (array ) array_map (' intval ' , $ detach );
132
+ $ changes ['detached ' ] = (array ) array_map (function ( $ v ) { return ( int ) $ v ; } , $ detach );
117
133
}
118
134
119
135
// 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)
156
172
{
157
173
if ($ id instanceof Model)
158
174
{
159
- $ model = $ id ; $ id = $ model ->getKey ();
160
- }
175
+ $ model = $ id ;
161
176
162
- $ ids = ( array ) $ id ;
177
+ $ id = $ model -> getKey () ;
163
178
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.
173
180
$ model ->push ($ this ->foreignKey , $ this ->parent ->getKey (), true );
174
181
}
175
182
else
176
183
{
177
184
$ query = $ this ->newRelatedQuery ();
178
185
179
- // Select related models.
180
- $ query ->whereIn ($ this ->related ->getKeyName (), $ ids );
186
+ $ query ->whereIn ($ this ->related ->getKeyName (), (array ) $ id );
181
187
182
188
// Attach the new parent id to the related model.
183
189
$ query ->push ($ this ->foreignKey , $ this ->parent ->getKey (), true );
184
190
}
185
191
192
+ // Attach the new ids to the parent model.
193
+ $ this ->parent ->push ($ this ->otherKey , (array ) $ id , true );
194
+
186
195
if ($ touch ) $ this ->touchIfTouching ();
187
196
}
188
197
@@ -238,9 +247,9 @@ protected function buildDictionary(Collection $results)
238
247
239
248
foreach ($ results as $ result )
240
249
{
241
- foreach ($ result ->$ foreign as $ single )
250
+ foreach ($ result ->$ foreign as $ item )
242
251
{
243
- $ dictionary [$ single ][] = $ result ;
252
+ $ dictionary [$ item ][] = $ result ;
244
253
}
245
254
}
246
255
0 commit comments