[英]concat collection in Laravel 5.3
我试图将一组集合连接到另一组集合。 我在Laravel 5.5中看到它具有concat
函数,但是出于兼容性原因,我必须使用直到Laravel 5.3。
我尝试了merge
功能,但它不是串联而是合并。
还有其他解决方法,或者如何在不更新整个Laravel软件包的情况下更新Laravel集合?
您可以通过“宏”向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);
从5.5复制该方法。
如果有帮助,我通常会在简短的博客文章中介绍Laravel中的宏:
Laravel 5.5 Docs-集合-扩展集合尽管这是从5.5 docs来的,但Collection
暂时具有此宏功能。
好的,没关系,我正在使用以下代码作为解决方法,
$first_collection->each(function($element) use (&$second_collection) {
$second_collection->push($element);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.