Skip to content

Commit 1b71ae4

Browse files
authored
Allow to sync a single model instance
Fix `sync()` method not handling properly single model's instances passed via `$ids` argument
1 parent f4c448f commit 1b71ae4

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Relations/BelongsToMany.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Model as EloquentModel;
99
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
1010
use Illuminate\Support\Arr;
11+
use Jenssegers\Mongodb\Eloquent\Model as MongodbModel;
1112

1213
class BelongsToMany extends EloquentBelongsToMany
1314
{
@@ -122,7 +123,9 @@ public function sync($ids, $detaching = true)
122123

123124
if ($ids instanceof Collection) {
124125
$ids = $ids->modelKeys();
125-
}
126+
} else if ($ids instanceof MongodbModel) {
127+
$ids = [$ids->{$this->relatedKey}];
128+
}
126129

127130
// First we need to attach any of the associated models that are not currently
128131
// in this joining table. We'll spin through the given IDs, checking to see

0 commit comments

Comments
 (0)