@@ -336,6 +336,49 @@ public function testDetachMethod()
336336 $ this ->assertCount (0 , $ post ->tags );
337337 }
338338
339+ public function testDetachMethodWithCustomPivot ()
340+ {
341+ $ post = Post::create (['title ' => Str::random ()]);
342+
343+ $ tag = Tag::create (['name ' => Str::random ()]);
344+ $ tag2 = Tag::create (['name ' => Str::random ()]);
345+ $ tag3 = Tag::create (['name ' => Str::random ()]);
346+ $ tag4 = Tag::create (['name ' => Str::random ()]);
347+ $ tag5 = Tag::create (['name ' => Str::random ()]);
348+ Tag::create (['name ' => Str::random ()]);
349+ Tag::create (['name ' => Str::random ()]);
350+
351+ $ post ->tagsWithCustomPivot ()->attach (Tag::all ());
352+
353+ $ this ->assertEquals (Tag::pluck ('name ' ), $ post ->tags ->pluck ('name ' ));
354+
355+ $ post ->tagsWithCustomPivot ()->detach ($ tag ->id );
356+ $ post ->load ('tagsWithCustomPivot ' );
357+ $ this ->assertEquals (
358+ Tag::whereNotIn ('id ' , [$ tag ->id ])->pluck ('name ' ),
359+ $ post ->tagsWithCustomPivot ->pluck ('name ' )
360+ );
361+
362+ $ post ->tagsWithCustomPivot ()->detach ([$ tag2 ->id , $ tag3 ->id ]);
363+ $ post ->load ('tagsWithCustomPivot ' );
364+ $ this ->assertEquals (
365+ Tag::whereNotIn ('id ' , [$ tag ->id , $ tag2 ->id , $ tag3 ->id ])->pluck ('name ' ),
366+ $ post ->tagsWithCustomPivot ->pluck ('name ' )
367+ );
368+
369+ $ post ->tagsWithCustomPivot ()->detach (new Collection ([$ tag4 , $ tag5 ]));
370+ $ post ->load ('tagsWithCustomPivot ' );
371+ $ this ->assertEquals (
372+ Tag::whereNotIn ('id ' , [$ tag ->id , $ tag2 ->id , $ tag3 ->id , $ tag4 ->id , $ tag5 ->id ])->pluck ('name ' ),
373+ $ post ->tagsWithCustomPivot ->pluck ('name ' )
374+ );
375+
376+ $ this ->assertCount (2 , $ post ->tagsWithCustomPivot );
377+ $ post ->tagsWithCustomPivot ()->detach ();
378+ $ post ->load ('tagsWithCustomPivot ' );
379+ $ this ->assertCount (0 , $ post ->tagsWithCustomPivot );
380+ }
381+
339382 public function testFirstMethod ()
340383 {
341384 $ post = Post::create (['title ' => Str::random ()]);
0 commit comments