php 如何在 Laravel 中正确合并多个集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32387575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly merge multiple collections in Laravel
提问by Marcel
I want to merge multiple collections into one. I do have a solution, which is the following:
我想将多个集合合并为一个。我确实有一个解决方案,如下所示:
$allItems = $collection1->merge($collection2)
->merge($collection3)
->merge($collection4)
->merge($collection5);
This actually does work, but I run into problems in cases where some or all of the collections contain no objects. I get an error along the lines of call to merge() on non object
.
这实际上确实有效,但是在某些或所有集合不包含对象的情况下我会遇到问题。我在call to merge() on non object
.
I actually tried to create an array of all of the collections, and then iterate through them, while checking their validity, but it didn't work and I feel it wasn't very elegant.
我实际上尝试创建所有集合的数组,然后遍历它们,同时检查它们的有效性,但它不起作用,我觉得它不是很优雅。
How can I elegantly iterate through this process of merging multiple collections, while taking into account that some or all of the collections might be empty or invalid? Appreciated!
如何优雅地遍历合并多个集合的过程,同时考虑到部分或全部集合可能为空或无效?赞赏!
回答by Marcel
What I ended up doing was separating each step. It was the merge chaining that was killing it, because some or all of the collections could have been invalid.
我最终做的是分离每一步。是合并链杀死了它,因为部分或全部集合可能是无效的。
$allItems = new \Illuminate\Database\Eloquent\Collection; //Create empty collection which we know has the merge() method
$allItems = $allItems->merge($collection1);
$allItems = $allItems->merge($collection2);
$allItems = $allItems->merge($collection3);
$allItems = $allItems->merge($collection4);
回答by Leonid Dashko
I had the same question, so I solved it in the following way:
我有同样的问题,所以我通过以下方式解决了它:
$clients = ClientAccount::query()->get();
$admins = AdminAccount::query()->get();
$users = collect($clients)->merge($admins)->merge($anotherCollection)->merge(...);
回答by wayne
depend on your data, if collection is actually null or your php support it you can do:
取决于您的数据,如果集合实际上为 null 或您的 php 支持它,您可以执行以下操作:
$allItems = $collection1->merge($collection2 ?: collect())
->merge($collection3 ?: collect())
->merge($collection4 ?: collect())
->merge($collection5 ?: collect());
or you want to do a reduce:
或者你想做一个减少:
$allItems = collect([$collection2, $collection3, $collection4])->reduce(function($arr, $item) {
if (empty($item) || $item->isEmpty())
return $arr;
return $arr->merge($item);
}, $collection1);
or plain php reduce without overhead
或普通的 php 减少没有开销
$allItems = array_reduce([
$collection2,
$collection3,
$collection4
], function($arr, $item) {
if (empty($item) || $item->isEmpty())
return $arr;
return $arr->merge($item);
}, $collection1);
回答by hawx
You could also try:
你也可以试试:
$allItems = collect([
'collection_1_key' => $collection_1,
'collection_2_key' => $collection_2,
'collection_3_key' => $collection_3,
'collection_4_key' => $collection_4,
]);
回答by Aruna Perera
I used ->merge()
but faced a small issue. So I'm adding this in case anyone else face the same issue. ->concat()
is a better solution on database collection merging.
我用过,->merge()
但遇到了一个小问题。所以我添加这个以防其他人面临同样的问题。->concat()
是数据库集合合并的更好解决方案。
$allItems = new \Illuminate\Database\Eloquent\Collection;
$allItems = $allItems->concat($collection1);
$allItems = $allItems->concat($collection2);
The reason for this is when merging they take ID of the tableas the key. So if two tables have two records with same table id ->merge()
will add only one record to $allItems
. But ->concat()
will add two records as they should.
这样做的原因是在合并时它们以表的 ID作为键。因此,如果两个表有两个具有相同表 ID 的->merge()
记录,则只会将一条记录添加到$allItems
. 但是->concat()
会添加两条记录。