Framework: Collection merge behavior changed

Created on 3 Feb 2014  ·  3Comments  ·  Source: laravel/framework

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

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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

iivanov2 picture iivanov2  ·  3Comments

PhiloNL picture PhiloNL  ·  3Comments

shopblocks picture shopblocks  ·  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  ·  3Comments