Hello,
I've been using Collection->merge()
to duplicate one collection object into himself in order to have each item two times in my Collection.
This code was pretty simple :
// Duplicate the collections
$words = Word::where('id', '<', '10');
$clone = clone $words;
$doubleWords = $words->merge($clone);
But since Laravel 4.1, the merge method returns me the only one of the two collections like if it was a simple $words + $clone
.
Do you have any idea on how to revert back to a similar behaviour while keeping a collection ?
Thank you for your answer and apologize for bad English
The reason for this is that Eloquent collections, upon merging, removes models with duplicate primary keys. You could iterate through $words
and do $words->push($word)
to add an extra reference to each model manually.
Thank you @anlutro for this explanation I was not able to figure out myself.
Thanks @anlutro, your explanation was a big help to me.
Most helpful comment
The reason for this is that Eloquent collections, upon merging, removes models with duplicate primary keys. You could iterate through
$words
and do$words->push($word)
to add an extra reference to each model manually.