回答編集履歴

2

文言加筆

2017/10/24 09:17

投稿

aro10
aro10

スコア4106

test CHANGED
@@ -16,11 +16,11 @@
16
16
 
17
17
  ```
18
18
 
19
- //以下は結果が違う
19
+ //以下は添字配列の場合は、結果が違う
20
20
 
21
21
  $items = $data1->merge($data2); //キー重複分が上書き
22
22
 
23
- $items = collect($data1)->merge($data2);
23
+ $items = collect($data1)->merge($data2); //array_mergeを内部で利用
24
24
 
25
25
 
26
26
 

1

追記

2017/10/24 09:17

投稿

aro10
aro10

スコア4106

test CHANGED
@@ -3,3 +3,27 @@
3
3
  $result1->concat($result2);
4
4
 
5
5
  [Laravel 5.5 コレクション concat()](https://readouble.com/laravel/5.5/ja/collections.html#method-concat)
6
+
7
+
8
+
9
+ Illuminate/SupportのCollectionクラスと、それを継承するlluminate/Database/EloquentのCollectionでは、mergeメソッドの実装が異なっているみたいなので注意が必要ですね。
10
+
11
+ [Illuminate/Support\Collection](https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Collection.php)
12
+
13
+ [lluminate/Database/Eloquent](https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Eloquent/Collection.php)
14
+
15
+
16
+
17
+ ```
18
+
19
+ //以下は結果が違う
20
+
21
+ $items = $data1->merge($data2); //キー重複分が上書き
22
+
23
+ $items = collect($data1)->merge($data2);
24
+
25
+
26
+
27
+ dd($items->toArray());
28
+
29
+ ```