5
5
use Illuminate \Database \Eloquent \Builder ;
6
6
use Illuminate \Database \Eloquent \Collection ;
7
7
use Illuminate \Database \Eloquent \Model ;
8
+ use Illuminate \Database \Eloquent \Model as EloquentModel ;
8
9
use Illuminate \Database \Eloquent \Relations \BelongsToMany as EloquentBelongsToMany ;
9
10
use Illuminate \Support \Arr ;
10
- use Illuminate \Database \Eloquent \Model as EloquentModel ;
11
11
12
12
class BelongsToMany extends EloquentBelongsToMany
13
13
{
@@ -34,7 +34,13 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
34
34
*/
35
35
protected function hydratePivotRelation (array $ models )
36
36
{
37
- // Do nothing.
37
+ foreach ($ models as $ model ) {
38
+ $ keyToUse = $ this ->getTable () == $ model ->getTable () ? $ this ->getForeignKey () : $ this ->getRelatedKey ();
39
+ $ pcontent = $ model ->{$ keyToUse };
40
+ $ model ->setRelation ($ this ->accessor , $ this ->newExistingPivot (
41
+ $ pcontent [0 ]
42
+ ));
43
+ }
38
44
}
39
45
40
46
/**
@@ -74,8 +80,10 @@ public function addConstraints()
74
80
protected function setWhere ()
75
81
{
76
82
$ foreign = $ this ->getForeignKey ();
77
-
78
- $ this ->query ->where ($ foreign , '= ' , $ this ->parent ->getKey ());
83
+ $ key = $ this ->parent ->getKey ();
84
+ $ this ->query
85
+ ->where ($ foreign , '= ' , $ key )
86
+ ->orWhereRaw ([$ foreign .'._id ' => $ key ]);
79
87
80
88
return $ this ;
81
89
}
@@ -132,12 +140,17 @@ public function sync($ids, $detaching = true)
132
140
// See issue #256.
133
141
if ($ current instanceof Collection) {
134
142
$ current = $ ids ->modelKeys ();
143
+ } elseif (is_array ($ current )) {
144
+ foreach ($ current as $ key => $ value ) {
145
+ if ($ value ['_id ' ]) {
146
+ $ current [$ key ] = $ value ['_id ' ];
147
+ }
148
+ }
135
149
}
136
150
137
151
$ records = $ this ->formatSyncList ($ ids );
138
152
139
153
$ current = Arr::wrap ($ current );
140
-
141
154
$ detach = array_diff ($ current , array_keys ($ records ));
142
155
143
156
// We need to make sure we pass a clean array, so that it is not interpreted
@@ -188,7 +201,7 @@ public function attach($id, array $attributes = [], $touch = true)
188
201
$ id = $ model ->getKey ();
189
202
190
203
// Attach the new parent id to the related model.
191
- $ model ->push ($ this ->foreignPivotKey , $ this ->parent ->getKey (), true );
204
+ $ model ->push ($ this ->foreignPivotKey , array_merge ( $ attributes , [ ' _id ' => $ this ->parent ->getKey ()] ), true );
192
205
} else {
193
206
if ($ id instanceof Collection) {
194
207
$ id = $ id ->modelKeys ();
@@ -199,11 +212,17 @@ public function attach($id, array $attributes = [], $touch = true)
199
212
$ query ->whereIn ($ this ->related ->getKeyName (), (array ) $ id );
200
213
201
214
// Attach the new parent id to the related model.
202
- $ query ->push ($ this ->foreignPivotKey , $ this ->parent ->getKey (), true );
215
+ $ query ->push ($ this ->foreignPivotKey , array_merge ($ attributes , ['_id ' => $ this ->parent ->getKey ()]), true );
216
+ }
217
+
218
+ //Pivot Collection
219
+ $ pivot_x = [];
220
+ foreach ((array )$ id as $ item ) {
221
+ $ pivot_x [] = array_merge ($ attributes , ['_id ' => $ item ]);
203
222
}
204
223
205
224
// Attach the new ids to the parent model.
206
- $ this ->parent ->push ($ this ->getRelatedKey (), ( array ) $ id , true );
225
+ $ this ->parent ->push ($ this ->getRelatedKey (), $ pivot_x , true );
207
226
208
227
if ($ touch ) {
209
228
$ this ->touchIfTouching ();
@@ -227,15 +246,15 @@ public function detach($ids = [], $touch = true)
227
246
$ ids = (array ) $ ids ;
228
247
229
248
// Detach all ids from the parent model.
230
- $ this ->parent ->pull ($ this ->getRelatedKey (), $ ids );
249
+ $ this ->parent ->pull ($ this ->getRelatedKey (), [ ' _id ' => $ ids] );
231
250
232
251
// Prepare the query to select all related objects.
233
252
if (count ($ ids ) > 0 ) {
234
253
$ query ->whereIn ($ this ->related ->getKeyName (), $ ids );
235
254
}
236
255
237
256
// Remove the relation to the parent.
238
- $ query ->pull ($ this ->foreignPivotKey , $ this ->parent ->getKey ());
257
+ $ query ->pull ($ this ->foreignPivotKey , [ ' _id ' => $ this ->parent ->getKey ()] );
239
258
240
259
if ($ touch ) {
241
260
$ this ->touchIfTouching ();
0 commit comments