如何合并两个雄辩的集合而又不丢失任何数据和负载关系?
//I have two collections
$e1=Colour::find(1,3,7);
$e2=Colour::find(31,33,88);
//I need the following output
$merged=$e1->merge($e2)->load('relation');
执行上述合并时,第一个集合将覆盖第二个集合。
请给我一个解决方案。
答案 0 :(得分:0)
使用Laravel集合
https://laravel.com/docs/5.7/collections#method-put
代码示例:
$collection = collect(['value' => $e1->value]);
$collection->put('value', $e2->value);
$collection->all();
答案 1 :(得分:0)
将$e2
的元素推到$e1
集合,然后根据需要使用$e1
。
foreach ($e2 as $e) {
$e1->push($e);
}
$e1->load('relation');