質問するログイン新規登録

回答編集履歴

2

文言加筆

2017/10/24 09:17

投稿

aro10
aro10

スコア4106

answer CHANGED
@@ -7,9 +7,9 @@
7
7
  [lluminate/Database/Eloquent](https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Eloquent/Collection.php)
8
8
 
9
9
  ```
10
- //以下は結果が違う
10
+ //以下は添字配列の場合は、結果が違う
11
11
  $items = $data1->merge($data2); //キー重複分が上書き
12
- $items = collect($data1)->merge($data2);
12
+ $items = collect($data1)->merge($data2); //array_mergeを内部で利用
13
13
 
14
14
  dd($items->toArray());
15
15
  ```

1

追記

2017/10/24 09:17

投稿

aro10
aro10

スコア4106

answer CHANGED
@@ -1,3 +1,15 @@
1
1
  Laravel5.5で、コレクションのconcatメソッドを利用すると2つが結合したデータを得られるかと思います。
2
2
  $result1->concat($result2);
3
- [Laravel 5.5 コレクション concat()](https://readouble.com/laravel/5.5/ja/collections.html#method-concat)
3
+ [Laravel 5.5 コレクション concat()](https://readouble.com/laravel/5.5/ja/collections.html#method-concat)
4
+
5
+ Illuminate/SupportのCollectionクラスと、それを継承するlluminate/Database/EloquentのCollectionでは、mergeメソッドの実装が異なっているみたいなので注意が必要ですね。
6
+ [Illuminate/Support\Collection](https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Collection.php)
7
+ [lluminate/Database/Eloquent](https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Eloquent/Collection.php)
8
+
9
+ ```
10
+ //以下は結果が違う
11
+ $items = $data1->merge($data2); //キー重複分が上書き
12
+ $items = collect($data1)->merge($data2);
13
+
14
+ dd($items->toArray());
15
+ ```