如何合并两个雄辩的和负载的关系

时间:2019-10-30 07:27:25

标签: laravel merge eloquent

如何合并两个雄辩的集合而又不丢失任何数据和负载关系?

//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');

执行上述合并时,第一个集合将覆盖第二个集合。

请给我一个解决方案。

2 个答案:

答案 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');