File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Spatie \CollectionMacros \Macros ;
4
4
5
+ use Illuminate \Support \Arr ;
5
6
use Illuminate \Support \Collection ;
6
7
7
8
/**
@@ -19,7 +20,7 @@ class CollectBy
19
20
public function __invoke ()
20
21
{
21
22
return function ($ key , $ default = null ): Collection {
22
- return new static ($ this ->get ( $ key , $ default ));
23
+ return new static (Arr:: get ( $ this ->items , $ key , $ default ));
23
24
};
24
25
}
25
26
}
Original file line number Diff line number Diff line change 60
60
61
61
expect ($ ingredients )->toEqual (new Collection ());
62
62
});
63
+
64
+ it ('collects path from collection using dot notation ' , function () {
65
+ $ collection = new Collection ([
66
+ 'baz.qux ' => 'quux ' ,
67
+ 'foo ' => [
68
+ 'bar ' => [
69
+ 'baz ' => 100 ,
70
+ ],
71
+ ],
72
+ ]);
73
+
74
+ expect ($ collection ->collectBy ('foo.bar ' ))->toBeInstanceOf (Collection::class);
75
+ expect ($ collection ->collectBy ('foo.bar ' )->toArray ())->toEqual ([
76
+ 'baz ' => 100 ,
77
+ ]);
78
+
79
+ expect ($ collection ->collectBy ('baz.qux ' ))->toBeInstanceOf (Collection::class);
80
+ expect ($ collection ->collectBy ('baz.qux ' )->toArray ())->toEqual (['quux ' ]);
81
+ });
You can’t perform that action at this time.
0 commit comments