55use Illuminate \Contracts \Queue \QueueableCollection ;
66use Illuminate \Contracts \Queue \QueueableEntity ;
77use Illuminate \Contracts \Support \Arrayable ;
8+ use Illuminate \Database \Eloquent \Relations \Concerns \InteractsWithDictionary ;
89use Illuminate \Support \Arr ;
910use Illuminate \Support \Collection as BaseCollection ;
1011use LogicException ;
1718 */
1819class Collection extends BaseCollection implements QueueableCollection
1920{
21+ use InteractsWithDictionary;
22+
2023 /**
2124 * Find a model in the collection by key.
2225 *
@@ -322,7 +325,7 @@ public function merge($items)
322325 $ dictionary = $ this ->getDictionary ();
323326
324327 foreach ($ items as $ item ) {
325- $ dictionary [$ item ->getKey ()] = $ item ;
328+ $ dictionary [$ this -> getDictionaryKey ( $ item ->getKey () )] = $ item ;
326329 }
327330
328331 return new static (array_values ($ dictionary ));
@@ -398,7 +401,7 @@ public function diff($items)
398401 $ dictionary = $ this ->getDictionary ($ items );
399402
400403 foreach ($ this ->items as $ item ) {
401- if (! isset ($ dictionary [$ item ->getKey ()])) {
404+ if (! isset ($ dictionary [$ this -> getDictionaryKey ( $ item ->getKey () )])) {
402405 $ diff ->add ($ item );
403406 }
404407 }
@@ -423,7 +426,7 @@ public function intersect($items)
423426 $ dictionary = $ this ->getDictionary ($ items );
424427
425428 foreach ($ this ->items as $ item ) {
426- if (isset ($ dictionary [$ item ->getKey ()])) {
429+ if (isset ($ dictionary [$ this -> getDictionaryKey ( $ item ->getKey () )])) {
427430 $ intersect ->add ($ item );
428431 }
429432 }
@@ -459,7 +462,7 @@ public function only($keys)
459462 return new static ($ this ->items );
460463 }
461464
462- $ dictionary = Arr::only ($ this ->getDictionary (), $ keys );
465+ $ dictionary = Arr::only ($ this ->getDictionary (), array_map ( $ this -> getDictionaryKey (...), ( array ) $ keys) );
463466
464467 return new static (array_values ($ dictionary ));
465468 }
@@ -472,7 +475,7 @@ public function only($keys)
472475 */
473476 public function except ($ keys )
474477 {
475- $ dictionary = Arr::except ($ this ->getDictionary (), $ keys );
478+ $ dictionary = Arr::except ($ this ->getDictionary (), array_map ( $ this -> getDictionaryKey (...), ( array ) $ keys) );
476479
477480 return new static (array_values ($ dictionary ));
478481 }
@@ -545,7 +548,7 @@ public function getDictionary($items = null)
545548 $ dictionary = [];
546549
547550 foreach ($ items as $ value ) {
548- $ dictionary [$ value ->getKey ()] = $ value ;
551+ $ dictionary [$ this -> getDictionaryKey ( $ value ->getKey () )] = $ value ;
549552 }
550553
551554 return $ dictionary ;
0 commit comments