[英]concat collection in Laravel 5.3
I'm trying to concatenate a set collection to another set of collection. 我试图将一组集合连接到另一组集合。 I see in Laravel 5.5 it has
concat
function, but for compatibility reason I have to use until Laravel 5.3 only. 我在Laravel 5.5中看到它具有
concat
函数,但是出于兼容性原因,我必须使用直到Laravel 5.3。
I tried merge
function but it is not concatenating but merging instead. 我尝试了
merge
功能,但它不是串联而是合并。
What are other workaround, or how can I update the Laravel Collection without update the whole Laravel package? 还有其他解决方法,或者如何在不更新整个Laravel软件包的情况下更新Laravel集合?
You can add functionality to Illuminate\\Support\\Collection
via "macro"s if you want to: 您可以通过“宏”向
Illuminate\\Support\\Collection
添加功能,如果您要:
\Illuminate\Support\Collection::macro('concat', function ($source) {
$result = new static($this);
foreach ($source as $item) {
$result->push($item);
}
return $result;
});
$new = $someCollection->concat($otherOne);
Copied the method from 5.5. 从5.5复制该方法。
I have a short blog post about macros in Laravel in general, if it helps: 如果有帮助,我通常会在简短的博客文章中介绍Laravel中的宏:
asklagbox blog - Laravel macros asklagbox博客-Laravel宏
Laravel 5.5 Docs - Collections - Extending Collections Though this is from 5.5 docs Collection
has had this macro functionality for awhile now. Laravel 5.5 Docs-集合-扩展集合尽管这是从5.5 docs来的,但
Collection
暂时具有此宏功能。
Okay nevermind, I'm using code below as workaround, 好的,没关系,我正在使用以下代码作为解决方法,
$first_collection->each(function($element) use (&$second_collection) {
$second_collection->push($element);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.