将具有缺失值的集合合并为laravel

时间:2019-04-26 11:26:39

标签: mysql laravel laravel-5 collections

我正在尝试将缺少的值添加到导致我的数据库表包含多个项目的集合中。

我试图尝试的是将集合中缺少的项目添加为联合值。

Collection {#217 ▼

 #items: array:7 [▼

0 => {#397 ▼

  +"item_id": 1

  +"amount": "4"

}

1 => {#402 ▼

  +"item_id": 3

  +"amount": "1"

}

2 => {#396 ▶}

3 => {#213 ▶}

4 => {#399 ▶}

5 => {#400 ▶}

6 => {#398 ▶}

]

}

我使用了联合,但无法正常工作,它创建了一个正确的数组

$union = $collection->union([2 => ['0']]);

我不想使用单独的数组和重复项。

1 个答案:

答案 0 :(得分:0)

尝试

$union = $collection->merge([2 => ['0']]);