@@ -85,7 +85,7 @@ public function addEagerConstraints(array $models)
8585 $ modelKeys = $ this ->getCompositeKeys ($ models , $ this ->parentKey );
8686 $ qualifiedKeys = $ this ->qualifyPivotColumn ($ this ->foreignPivotKey );
8787
88- $ query = ( method_exists ( $ this , ' getRelationQuery ' ) ? $ this ->getRelationQuery () : null ) ?? $ this ->query ;
88+ $ query = $ this ->getRelationQuery () ?? $ this ->query ;
8989 $ query ->{$ whereIn }($ qualifiedKeys , $ modelKeys );
9090
9191 if ($ modelKeys === []) {
@@ -184,10 +184,8 @@ protected function buildDictionary(Collection $results)
184184
185185 $ dictionary = [];
186186
187- $ accessor = property_exists ($ this , 'accessor ' ) ? $ this ->accessor : 'pivot ' ;
188-
189187 foreach ($ results as $ result ) {
190- $ key = $ this ->buildDictionaryKey ($ result ->{$ accessor }, $ this ->foreignPivotKey );
188+ $ key = $ this ->buildDictionaryKey ($ result ->{$ this -> accessor }, $ this ->foreignPivotKey );
191189 $ dictionary [$ key ][] = $ result ;
192190 }
193191
@@ -322,9 +320,7 @@ protected function baseAttachRecord($id, $timed)
322320 $ record = $ this ->addTimestampsToAttachment ($ record );
323321 }
324322
325- $ pivotValues = property_exists ($ this , 'pivotValues ' ) ? $ this ->pivotValues : [];
326-
327- foreach ($ pivotValues as $ value ) {
323+ foreach ($ this ->pivotValues as $ value ) {
328324 $ record [$ value ['column ' ]] = $ value ['value ' ];
329325 }
330326
@@ -350,9 +346,7 @@ protected function formatAttachRecords($ids, array $attributes)
350346 $ hasTimestamps = $ this ->hasPivotColumn ($ this ->createdAt ())
351347 || $ this ->hasPivotColumn ($ this ->updatedAt ());
352348
353- $ castedAttributes = method_exists ($ this , 'castAttributes ' )
354- ? $ this ->castAttributes ($ attributes )
355- : $ attributes ;
349+ $ castedAttributes = $ this ->castAttributes ($ attributes );
356350
357351 foreach ($ ids as $ value ) {
358352 $ records [] = array_merge (
@@ -437,22 +431,16 @@ public function newPivotQuery()
437431
438432 $ query = $ this ->newPivotStatement ();
439433
440- if (property_exists ($ this , 'pivotWheres ' )) {
441- foreach ($ this ->pivotWheres as $ arguments ) {
442- $ query ->where (...$ arguments );
443- }
434+ foreach ($ this ->pivotWheres as $ arguments ) {
435+ $ query ->where (...$ arguments );
444436 }
445437
446- if (property_exists ($ this , 'pivotWhereIns ' )) {
447- foreach ($ this ->pivotWhereIns as $ arguments ) {
448- $ query ->whereIn (...$ arguments );
449- }
438+ foreach ($ this ->pivotWhereIns as $ arguments ) {
439+ $ query ->whereIn (...$ arguments );
450440 }
451441
452- if (property_exists ($ this , 'pivotWhereNulls ' )) {
453- foreach ($ this ->pivotWhereNulls as $ arguments ) {
454- $ query ->whereNull (...$ arguments );
455- }
442+ foreach ($ this ->pivotWhereNulls as $ arguments ) {
443+ $ query ->whereNull (...$ arguments );
456444 }
457445
458446 foreach ($ this ->foreignPivotKey as $ index => $ key ) {
@@ -498,10 +486,13 @@ public function detach($ids = null, $touch = true)
498486 return parent ::detach ($ ids , $ touch );
499487 }
500488
501- $ using = property_exists ($ this , 'using ' ) ? $ this ->using : null ;
489+ if ($ this ->using ) {
490+ $ results = 0 ;
491+ $ records = $ this ->getCurrentlyAttachedPivotsForIds ($ ids );
502492
503- if ($ using && method_exists ($ this , 'detachUsingCustomClass ' )) {
504- $ results = $ this ->detachUsingCustomClass ($ ids );
493+ foreach ($ records as $ record ) {
494+ $ results += $ record ->delete ();
495+ }
505496 } else {
506497 $ query = $ this ->newPivotQuery ();
507498
@@ -542,22 +533,15 @@ public function newPivot(array $attributes = [], $exists = false)
542533 return parent ::newPivot ($ attributes , $ exists );
543534 }
544535
545- $ pivotValues = property_exists ($ this , 'pivotValues ' ) ? $ this ->pivotValues : [];
546- $ attributes = array_merge (array_column ($ pivotValues , 'value ' , 'column ' ), $ attributes );
547-
548- $ using = property_exists ($ this , 'using ' ) ? $ this ->using : null ;
536+ $ attributes = array_merge (array_column ($ this ->pivotValues , 'value ' , 'column ' ), $ attributes );
549537
550- $ pivot = $ using
551- ? $ using ::fromRawAttributes ($ this ->parent , $ attributes , $ this ->table , $ exists )
538+ $ pivot = $ this -> using
539+ ? $ this -> using ::fromRawAttributes ($ this ->parent , $ attributes , $ this ->table , $ exists )
552540 : Pivot::fromAttributes ($ this ->parent , $ attributes , $ this ->table , $ exists );
553541
554- $ pivot ->setPivotKeys ($ this ->foreignPivotKey , $ this ->relatedPivotKey );
555-
556- if (method_exists ($ pivot , 'setRelatedModel ' )) {
557- $ pivot ->setRelatedModel ($ this ->related );
558- }
559-
560- return $ pivot ;
542+ return $ pivot
543+ ->setPivotKeys ($ this ->foreignPivotKey , $ this ->relatedPivotKey )
544+ ->setRelatedModel ($ this ->related );
561545 }
562546
563547 /**
@@ -582,18 +566,13 @@ protected function getCurrentlyAttachedPivotsForIds($ids = null)
582566 })
583567 ->get ()
584568 ->map (function ($ record ) {
585- $ using = property_exists ($ this , 'using ' ) ? $ this ->using : null ;
586- $ class = $ using ?: Pivot::class;
569+ $ class = $ this ->using ?: Pivot::class;
587570
588571 $ pivot = $ class ::fromRawAttributes ($ this ->parent , (array ) $ record , $ this ->getTable (), true );
589572
590- $ pivot ->setPivotKeys ($ this ->foreignPivotKey , $ this ->relatedPivotKey );
591-
592- if (method_exists ($ pivot , 'setRelatedModel ' )) {
593- $ pivot ->setRelatedModel ($ this ->related );
594- }
595-
596- return $ pivot ;
573+ return $ pivot
574+ ->setPivotKeys ($ this ->foreignPivotKey , $ this ->relatedPivotKey )
575+ ->setRelatedModel ($ this ->related );
597576 });
598577 }
599578
@@ -771,10 +750,15 @@ public function attach($ids, array $attributes = [], $touch = true)
771750 return ;
772751 }
773752
774- $ using = property_exists ($ this , 'using ' ) ? $ this ->using : null ;
753+ if ($ this ->using ) {
754+ $ records = $ this ->formatAttachRecords (
755+ $ this ->parseIds ($ ids ),
756+ $ attributes
757+ );
775758
776- if ($ using && method_exists ($ this , 'attachUsingCustomClass ' )) {
777- $ this ->attachUsingCustomClass ($ ids , $ attributes );
759+ foreach ($ records as $ record ) {
760+ $ this ->newPivot ($ record , false )->save ();
761+ }
778762 } else {
779763 $ this ->newPivotStatement ()->insert ($ this ->formatAttachRecords (
780764 $ this ->parseIds ($ ids ),
0 commit comments