5
5
use Illuminate \Contracts \Queue \QueueableCollection ;
6
6
use Illuminate \Contracts \Queue \QueueableEntity ;
7
7
use Illuminate \Contracts \Support \Arrayable ;
8
+ use Illuminate \Database \Eloquent \Relations \Concerns \InteractsWithDictionary ;
8
9
use Illuminate \Support \Arr ;
9
10
use Illuminate \Support \Collection as BaseCollection ;
10
11
use LogicException ;
17
18
*/
18
19
class Collection extends BaseCollection implements QueueableCollection
19
20
{
21
+ use InteractsWithDictionary;
22
+
20
23
/**
21
24
* Find a model in the collection by key.
22
25
*
@@ -322,7 +325,7 @@ public function merge($items)
322
325
$ dictionary = $ this ->getDictionary ();
323
326
324
327
foreach ($ items as $ item ) {
325
- $ dictionary [$ item ->getKey ()] = $ item ;
328
+ $ dictionary [$ this -> getDictionaryKey ( $ item ->getKey () )] = $ item ;
326
329
}
327
330
328
331
return new static (array_values ($ dictionary ));
@@ -398,7 +401,7 @@ public function diff($items)
398
401
$ dictionary = $ this ->getDictionary ($ items );
399
402
400
403
foreach ($ this ->items as $ item ) {
401
- if (! isset ($ dictionary [$ item ->getKey ()])) {
404
+ if (! isset ($ dictionary [$ this -> getDictionaryKey ( $ item ->getKey () )])) {
402
405
$ diff ->add ($ item );
403
406
}
404
407
}
@@ -423,7 +426,7 @@ public function intersect($items)
423
426
$ dictionary = $ this ->getDictionary ($ items );
424
427
425
428
foreach ($ this ->items as $ item ) {
426
- if (isset ($ dictionary [$ item ->getKey ()])) {
429
+ if (isset ($ dictionary [$ this -> getDictionaryKey ( $ item ->getKey () )])) {
427
430
$ intersect ->add ($ item );
428
431
}
429
432
}
@@ -459,7 +462,7 @@ public function only($keys)
459
462
return new static ($ this ->items );
460
463
}
461
464
462
- $ dictionary = Arr::only ($ this ->getDictionary (), $ keys );
465
+ $ dictionary = Arr::only ($ this ->getDictionary (), array_map ( $ this -> getDictionaryKey (...), ( array ) $ keys) );
463
466
464
467
return new static (array_values ($ dictionary ));
465
468
}
@@ -472,7 +475,7 @@ public function only($keys)
472
475
*/
473
476
public function except ($ keys )
474
477
{
475
- $ dictionary = Arr::except ($ this ->getDictionary (), $ keys );
478
+ $ dictionary = Arr::except ($ this ->getDictionary (), array_map ( $ this -> getDictionaryKey (...), ( array ) $ keys) );
476
479
477
480
return new static (array_values ($ dictionary ));
478
481
}
@@ -545,7 +548,7 @@ public function getDictionary($items = null)
545
548
$ dictionary = [];
546
549
547
550
foreach ($ items as $ value ) {
548
- $ dictionary [$ value ->getKey ()] = $ value ;
551
+ $ dictionary [$ this -> getDictionaryKey ( $ value ->getKey () )] = $ value ;
549
552
}
550
553
551
554
return $ dictionary ;
0 commit comments