From 3e5e81ef58e539ad1b7b27b05196020a5c41d4ac Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Mon, 21 Jun 2021 16:51:14 -0300
Subject: [PATCH 1/8] add validate instaceof Model

---
 src/Relations/BelongsToMany.php | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php
index 915cc95e2..713f637f4 100644
--- a/src/Relations/BelongsToMany.php
+++ b/src/Relations/BelongsToMany.php
@@ -223,8 +223,12 @@ public function detach($ids = [], $touch = true)
         // We'll return the numbers of affected rows when we do the deletes.
         $ids = (array) $ids;
 
-        // Detach all ids from the parent model.
-        $this->parent->pull($this->getRelatedKey(), $ids);
+
+        if ($this->parent instanceof \Jenssegers\Mongodb\Eloquent\Model){
+            $this->parent->pull($this->getRelatedKey(), $ids);
+        }
+
+
 
         // Prepare the query to select all related objects.
         if (count($ids) > 0) {

From 7aa00bc9bad72566878300df42bd42edc49a5ab3 Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Mon, 21 Jun 2021 16:58:01 -0300
Subject: [PATCH 2/8] fix current in sync method for Hybrid

---
 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 713f637f4..a0715ec22 100644
--- a/src/Relations/BelongsToMany.php
+++ b/src/Relations/BelongsToMany.php
@@ -128,7 +128,7 @@ public function sync($ids, $detaching = true)
 
         // See issue #256.
         if ($current instanceof Collection) {
-            $current = $ids->modelKeys();
+            $current = $current->modelKeys();
         }
 
         $records = $this->formatSyncList($ids);

From a6ae7acae8b31ee0615558d3f3c1dc14f4f871f6 Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Fri, 2 Jul 2021 09:14:57 -0300
Subject: [PATCH 3/8] add test sync hybrid

---
 tests/HybridRelationsTest.php | 45 +++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php
index 7b4e7cdad..43ff166c4 100644
--- a/tests/HybridRelationsTest.php
+++ b/tests/HybridRelationsTest.php
@@ -194,4 +194,49 @@ public function testHybridWith()
                 $this->assertEquals($user->id, $user->books->count());
             });
     }
+    public function testHybridSync()
+    {
+        $user = new MysqlUser;
+        $this->assertInstanceOf(MysqlUser::class, $user);
+        $this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
+
+        // Mysql User
+        $user->name = 'John Doe';
+        $user->save();
+        $this->assertIsInt($user->id);
+        // SQL has many
+        $book = new Book(['title' => 'Harry Potter']);
+        $otherBook = new Book(['title' => 'Game of Thrones']);
+
+        $user->books()->sync([$book->id,$otherBook->id]);
+        $user = MysqlUser::find($user->id); // refetch
+        $this->assertCount(2, $user->books);
+
+        $user->books()->sync([$book->id]);
+        $user = MysqlUser::find($user->id); // refetch
+        $this->assertCount(1, $user->books);
+
+        $user->books()->sync([$book->id,$otherBook->id]);
+        $user = MysqlUser::find($user->id); // refetch
+        $this->assertCount(2, $user->books);
+        // MongoDB User
+        $user = new User;
+        $user->name = 'John Doe';
+        $user->save();
+        // MongoDB has many
+        $book = new MysqlBook(['title' => 'Harry Potter']);
+        $otherBook = new MysqlBook(['title' => 'Game of Thrones']);
+
+        $user->mysqlBooks()->sync([$book->id,$otherBook->id]);
+        $user = User::find($user->_id);
+        $this->assertCount(2, $user->mysqlBooks);
+
+        $user->mysqlBooks()->sync([$book->id]);
+        $user = User::find($user->_id);
+        $this->assertCount(1, $user->mysqlBooks);
+
+        $user->mysqlBooks()->sync([$book->id,$otherBook->id]);
+        $user = User::find($user->_id);
+        $this->assertCount(2, $user->mysqlBooks);
+    }
 }

From 74fba08432b99315a9ecf1d3cb99ed39c4dee5e8 Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Fri, 2 Jul 2021 09:21:24 -0300
Subject: [PATCH 4/8] fix to styleci

---
 src/Relations/BelongsToMany.php | 2 +-
 tests/HybridRelationsTest.php   | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php
index a0715ec22..99f37910d 100644
--- a/src/Relations/BelongsToMany.php
+++ b/src/Relations/BelongsToMany.php
@@ -224,7 +224,7 @@ public function detach($ids = [], $touch = true)
         $ids = (array) $ids;
 
 
-        if ($this->parent instanceof \Jenssegers\Mongodb\Eloquent\Model){
+        if ($this->parent instanceof \Jenssegers\Mongodb\Eloquent\Model) {
             $this->parent->pull($this->getRelatedKey(), $ids);
         }
 
diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php
index 43ff166c4..36ab12a4b 100644
--- a/tests/HybridRelationsTest.php
+++ b/tests/HybridRelationsTest.php
@@ -208,7 +208,7 @@ public function testHybridSync()
         $book = new Book(['title' => 'Harry Potter']);
         $otherBook = new Book(['title' => 'Game of Thrones']);
 
-        $user->books()->sync([$book->id,$otherBook->id]);
+        $user->books()->sync([$book->id, $otherBook->id]);
         $user = MysqlUser::find($user->id); // refetch
         $this->assertCount(2, $user->books);
 
@@ -216,7 +216,7 @@ public function testHybridSync()
         $user = MysqlUser::find($user->id); // refetch
         $this->assertCount(1, $user->books);
 
-        $user->books()->sync([$book->id,$otherBook->id]);
+        $user->books()->sync([$book->id, $otherBook->id]);
         $user = MysqlUser::find($user->id); // refetch
         $this->assertCount(2, $user->books);
         // MongoDB User
@@ -227,7 +227,7 @@ public function testHybridSync()
         $book = new MysqlBook(['title' => 'Harry Potter']);
         $otherBook = new MysqlBook(['title' => 'Game of Thrones']);
 
-        $user->mysqlBooks()->sync([$book->id,$otherBook->id]);
+        $user->mysqlBooks()->sync([$book->id, $otherBook->id]);
         $user = User::find($user->_id);
         $this->assertCount(2, $user->mysqlBooks);
 
@@ -235,7 +235,7 @@ public function testHybridSync()
         $user = User::find($user->_id);
         $this->assertCount(1, $user->mysqlBooks);
 
-        $user->mysqlBooks()->sync([$book->id,$otherBook->id]);
+        $user->mysqlBooks()->sync([$book->id, $otherBook->id]);
         $user = User::find($user->_id);
         $this->assertCount(2, $user->mysqlBooks);
     }

From 0c96f80958a5432921b6ab2dc8a2cf718050da2e Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Fri, 2 Jul 2021 09:23:23 -0300
Subject: [PATCH 5/8] fix styleci again

---
 src/Relations/BelongsToMany.php | 3 ---
 tests/HybridRelationsTest.php   | 1 +
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php
index 99f37910d..69918a7b4 100644
--- a/src/Relations/BelongsToMany.php
+++ b/src/Relations/BelongsToMany.php
@@ -223,13 +223,10 @@ public function detach($ids = [], $touch = true)
         // We'll return the numbers of affected rows when we do the deletes.
         $ids = (array) $ids;
 
-
         if ($this->parent instanceof \Jenssegers\Mongodb\Eloquent\Model) {
             $this->parent->pull($this->getRelatedKey(), $ids);
         }
 
-
-
         // Prepare the query to select all related objects.
         if (count($ids) > 0) {
             $query->whereIn($this->related->getKeyName(), $ids);
diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php
index 36ab12a4b..44bcf3150 100644
--- a/tests/HybridRelationsTest.php
+++ b/tests/HybridRelationsTest.php
@@ -194,6 +194,7 @@ public function testHybridWith()
                 $this->assertEquals($user->id, $user->books->count());
             });
     }
+    
     public function testHybridSync()
     {
         $user = new MysqlUser;

From 978fe2b8f1d09aca2e4844a4ee7508fb096a5e71 Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Fri, 2 Jul 2021 16:18:33 -0300
Subject: [PATCH 6/8] fix test sync hybrid

---
 tests/HybridRelationsTest.php | 74 +++++++++++++++++------------------
 tests/models/Client.php       |  5 +++
 tests/models/MysqlUser.php    | 12 ++++++
 3 files changed, 53 insertions(+), 38 deletions(-)

diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php
index 44bcf3150..235fe111c 100644
--- a/tests/HybridRelationsTest.php
+++ b/tests/HybridRelationsTest.php
@@ -9,7 +9,6 @@ class HybridRelationsTest extends TestCase
     public function setUp(): void
     {
         parent::setUp();
-
         MysqlUser::executeSchema();
         MysqlBook::executeSchema();
         MysqlRole::executeSchema();
@@ -194,50 +193,49 @@ public function testHybridWith()
                 $this->assertEquals($user->id, $user->books->count());
             });
     }
-    
     public function testHybridSync()
     {
         $user = new MysqlUser;
+        $otherUser = new MysqlUser;
         $this->assertInstanceOf(MysqlUser::class, $user);
         $this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
+        $this->assertInstanceOf(MysqlUser::class, $otherUser);
+        $this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection());
 
-        // Mysql User
-        $user->name = 'John Doe';
-        $user->save();
-        $this->assertIsInt($user->id);
-        // SQL has many
-        $book = new Book(['title' => 'Harry Potter']);
-        $otherBook = new Book(['title' => 'Game of Thrones']);
-
-        $user->books()->sync([$book->id, $otherBook->id]);
-        $user = MysqlUser::find($user->id); // refetch
-        $this->assertCount(2, $user->books);
-
-        $user->books()->sync([$book->id]);
-        $user = MysqlUser::find($user->id); // refetch
-        $this->assertCount(1, $user->books);
-
-        $user->books()->sync([$book->id, $otherBook->id]);
-        $user = MysqlUser::find($user->id); // refetch
-        $this->assertCount(2, $user->books);
-        // MongoDB User
-        $user = new User;
+        // Create Mysql Users
         $user->name = 'John Doe';
         $user->save();
-        // MongoDB has many
-        $book = new MysqlBook(['title' => 'Harry Potter']);
-        $otherBook = new MysqlBook(['title' => 'Game of Thrones']);
-
-        $user->mysqlBooks()->sync([$book->id, $otherBook->id]);
-        $user = User::find($user->_id);
-        $this->assertCount(2, $user->mysqlBooks);
-
-        $user->mysqlBooks()->sync([$book->id]);
-        $user = User::find($user->_id);
-        $this->assertCount(1, $user->mysqlBooks);
-
-        $user->mysqlBooks()->sync([$book->id, $otherBook->id]);
-        $user = User::find($user->_id);
-        $this->assertCount(2, $user->mysqlBooks);
+        $user = MysqlUser::find($user->id);
+        $otherUser->name = 'Maria Doe';
+        $otherUser->save();
+        // Create Mongodb Clients
+        $client = Client::create(['name' => 'Pork Pies Ltd.']);
+        $otherClient = Client::create(['name' => 'Pork Pies Ltd.']);
+
+        // sync 2 users
+        $client->usersMysql()->sync([$user->id, $otherUser->id]);
+        $client = Client::find($client->_id);
+        $this->assertEquals(2, $client->usersMysql->count());
+        // sync 1 user
+        $client->usersMysql()->sync([$user->id]);
+        $client = Client::find($client->_id);
+        $this->assertEquals(1, $client->usersMysql->count());
+        // sync 2 users again
+        $client->usersMysql()->sync([$user->id, $otherUser->id]);
+        $client = Client::find($client->_id);
+        $this->assertEquals(2, $client->usersMysql->count());
+
+        // sync 2 Clients
+        $user->clients()->sync([$client->_id,$otherClient->_id ]);
+        $user = MysqlUser::find($user->id);
+        $this->assertEquals(2, $user->clients->count());
+        // Sync 1 Client
+        $user->clients()->sync([$client->_id]);
+        $user = MysqlUser::find($user->id);
+        $this->assertEquals(1, $user->clients->count());
+        // Sync 2 Clients again
+        $user->clients()->sync([$client->_id,$otherClient->_id ]);
+        $user = MysqlUser::find($user->id);
+        $this->assertEquals(2, $user->clients->count());
     }
 }
diff --git a/tests/models/Client.php b/tests/models/Client.php
index 2c1388a6c..eace8ddfa 100644
--- a/tests/models/Client.php
+++ b/tests/models/Client.php
@@ -27,4 +27,9 @@ public function addresses(): HasMany
     {
         return $this->hasMany('Address', 'data.client_id', 'data.client_id');
     }
+
+    public function usersMysql(): BelongsToMany
+    {
+        return $this->belongsToMany('MysqlUser');
+    }
 }
diff --git a/tests/models/MysqlUser.php b/tests/models/MysqlUser.php
index 8c1393fd5..4a2a9f314 100644
--- a/tests/models/MysqlUser.php
+++ b/tests/models/MysqlUser.php
@@ -31,6 +31,11 @@ public function mysqlBooks(): HasMany
         return $this->hasMany(MysqlBook::class);
     }
 
+    public function clients()
+    {
+        return $this->belongsToMany('Client',null,'mysql_users_id','clients');
+    }
+
     /**
      * Check if we need to run the schema.
      */
@@ -46,5 +51,12 @@ public static function executeSchema(): void
                 $table->timestamps();
             });
         }
+        if (! $schema->hasTable('client_mysql_user')) {
+            Schema::connection('mysql')->create('client_mysql_user', function (Blueprint $table) {
+                $table->integer('mysql_user_id')->unsigned();
+                $table->string('client_id');
+                $table->primary(['mysql_user_id','client_id']);
+            });
+        }
     }
 }

From e59a8e5d720548b56b0666ff78f9fbd680bc0255 Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Fri, 2 Jul 2021 16:25:06 -0300
Subject: [PATCH 7/8] fix syleci

---
 tests/HybridRelationsTest.php | 5 +++--
 tests/models/MysqlUser.php    | 4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php
index 235fe111c..ab041ec10 100644
--- a/tests/HybridRelationsTest.php
+++ b/tests/HybridRelationsTest.php
@@ -193,6 +193,7 @@ public function testHybridWith()
                 $this->assertEquals($user->id, $user->books->count());
             });
     }
+
     public function testHybridSync()
     {
         $user = new MysqlUser;
@@ -226,7 +227,7 @@ public function testHybridSync()
         $this->assertEquals(2, $client->usersMysql->count());
 
         // sync 2 Clients
-        $user->clients()->sync([$client->_id,$otherClient->_id ]);
+        $user->clients()->sync([$client->_id, $otherClient->_id ]);
         $user = MysqlUser::find($user->id);
         $this->assertEquals(2, $user->clients->count());
         // Sync 1 Client
@@ -234,7 +235,7 @@ public function testHybridSync()
         $user = MysqlUser::find($user->id);
         $this->assertEquals(1, $user->clients->count());
         // Sync 2 Clients again
-        $user->clients()->sync([$client->_id,$otherClient->_id ]);
+        $user->clients()->sync([$client->_id, $otherClient->_id ]);
         $user = MysqlUser::find($user->id);
         $this->assertEquals(2, $user->clients->count());
     }
diff --git a/tests/models/MysqlUser.php b/tests/models/MysqlUser.php
index 4a2a9f314..b6f8fb26b 100644
--- a/tests/models/MysqlUser.php
+++ b/tests/models/MysqlUser.php
@@ -33,7 +33,7 @@ public function mysqlBooks(): HasMany
 
     public function clients()
     {
-        return $this->belongsToMany('Client',null,'mysql_users_id','clients');
+        return $this->belongsToMany('Client', null, 'mysql_users_id', 'clients');
     }
 
     /**
@@ -55,7 +55,7 @@ public static function executeSchema(): void
             Schema::connection('mysql')->create('client_mysql_user', function (Blueprint $table) {
                 $table->integer('mysql_user_id')->unsigned();
                 $table->string('client_id');
-                $table->primary(['mysql_user_id','client_id']);
+                $table->primary(['mysql_user_id', 'client_id']);
             });
         }
     }

From 26e77f60daca6820ef2c2f86b38bc14905e57357 Mon Sep 17 00:00:00 2001
From: junio hyago <juniohyago@gmail.com>
Date: Fri, 2 Jul 2021 16:26:41 -0300
Subject: [PATCH 8/8] fix syle

---
 tests/HybridRelationsTest.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php
index ab041ec10..5177347d7 100644
--- a/tests/HybridRelationsTest.php
+++ b/tests/HybridRelationsTest.php
@@ -227,7 +227,7 @@ public function testHybridSync()
         $this->assertEquals(2, $client->usersMysql->count());
 
         // sync 2 Clients
-        $user->clients()->sync([$client->_id, $otherClient->_id ]);
+        $user->clients()->sync([$client->_id, $otherClient->_id]);
         $user = MysqlUser::find($user->id);
         $this->assertEquals(2, $user->clients->count());
         // Sync 1 Client
@@ -235,7 +235,7 @@ public function testHybridSync()
         $user = MysqlUser::find($user->id);
         $this->assertEquals(1, $user->clients->count());
         // Sync 2 Clients again
-        $user->clients()->sync([$client->_id, $otherClient->_id ]);
+        $user->clients()->sync([$client->_id, $otherClient->_id]);
         $user = MysqlUser::find($user->id);
         $this->assertEquals(2, $user->clients->count());
     }