Skip to content

Commit c004cf6

Browse files
[Bug fix] Fix BelongToMany when using ObjectId in relation
1 parent 8333880 commit c004cf6

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Relations/BelongsToMany.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ public function attach($id, array $attributes = [], $touch = true)
202202

203203
// Attach the new ids to the parent model.
204204
if ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
205-
$this->parent->push($this->relatedPivotKey, (array) $id, true);
205+
if ($id instanceof \MongoDB\BSON\ObjectId) {
206+
$id = [$id];
207+
} else {
208+
$id = (array) $id;
209+
}
210+
211+
$this->parent->push($this->relatedPivotKey, $id, true);
206212
} else {
207213
$instance = new $this->related();
208214
$instance->forceFill([$this->relatedKey => $id]);

0 commit comments

Comments
 (0)