From 1b71ae46fe01c4fe0128be5cf67f4fc3e458acc8 Mon Sep 17 00:00:00 2001 From: Pavel Borunov <8665691+mrneatly@users.noreply.github.com> Date: Sat, 28 May 2022 11:29:28 +0300 Subject: [PATCH 1/2] Allow to sync a single model instance Fix `sync()` method not handling properly single model's instances passed via `$ids` argument --- src/Relations/BelongsToMany.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 2352adc50..ef2f3a1c5 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; use Illuminate\Support\Arr; +use Jenssegers\Mongodb\Eloquent\Model as MongodbModel; class BelongsToMany extends EloquentBelongsToMany { @@ -122,7 +123,9 @@ public function sync($ids, $detaching = true) if ($ids instanceof Collection) { $ids = $ids->modelKeys(); - } + } else if ($ids instanceof MongodbModel) { + $ids = [$ids->{$this->relatedKey}]; + } // First we need to attach any of the associated models that are not currently // in this joining table. We'll spin through the given IDs, checking to see From c5cdadbd51ea37781722ed8a746d5932805ff14e Mon Sep 17 00:00:00 2001 From: Pavel Borunov <8665691+mrneatly@users.noreply.github.com> Date: Sat, 28 May 2022 11:54:00 +0300 Subject: [PATCH 2/2] Fix code style --- src/Relations/BelongsToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index ef2f3a1c5..656043eaf 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -123,7 +123,7 @@ public function sync($ids, $detaching = true) if ($ids instanceof Collection) { $ids = $ids->modelKeys(); - } else if ($ids instanceof MongodbModel) { + } elseif ($ids instanceof MongodbModel) { $ids = [$ids->{$this->relatedKey}]; }