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.
I tried merge
function but it is not concatenating but merging instead.
What are other workaround, or how can I update the Laravel Collection without update the whole Laravel package?
You can add functionality to Illuminate\\Support\\Collection
via "macro"s if you want to:
\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.
I have a short blog post about macros in Laravel in general, if it helps:
asklagbox blog - Laravel macros
Laravel 5.5 Docs - Collections - Extending Collections Though this is from 5.5 docs Collection
has had this macro functionality for awhile now.
Okay nevermind, I'm using code below as workaround,
$first_collection->each(function($element) use (&$second_collection) {
$second_collection->push($element);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.