@@ -255,6 +255,36 @@ public function testBelongsToMany(): void
255255 $ this ->assertCount (1 , $ client ->users );
256256 }
257257
258+ public function testSyncBelongsToMany ()
259+ {
260+ $ user = User::create (['name ' => 'John Doe ' ]);
261+
262+ $ first = Client::query ()->create (['name ' => 'Hans ' ]);
263+ $ second = Client::query ()->create (['name ' => 'Thomas ' ]);
264+
265+ $ user ->load ('clients ' );
266+ self ::assertEmpty ($ user ->clients );
267+
268+ $ user ->clients ()->sync ($ first );
269+
270+ $ user ->load ('clients ' );
271+ self ::assertCount (1 , $ user ->clients );
272+ self ::assertTrue ($ user ->clients ->first ()->is ($ first ));
273+
274+ $ user ->clients ()->sync ($ second );
275+
276+ $ user ->load ('clients ' );
277+ self ::assertCount (1 , $ user ->clients );
278+ self ::assertTrue ($ user ->clients ->first ()->is ($ second ));
279+
280+ $ user ->clients ()->syncWithoutDetaching ($ first );
281+
282+ $ user ->load ('clients ' );
283+ self ::assertCount (2 , $ user ->clients );
284+ self ::assertTrue ($ user ->clients ->first ()->is ($ first ));
285+ self ::assertTrue ($ user ->clients ->last ()->is ($ second ));
286+ }
287+
258288 public function testBelongsToManyAttachesExistingModels (): void
259289 {
260290 $ user = User::create (['name ' => 'John Doe ' , 'client_ids ' => ['1234523 ' ]]);
0 commit comments