Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection merge behavior changed #3445

Closed
RomainSauvaire opened this issue Feb 3, 2014 · 3 comments
Closed

Collection merge behavior changed #3445

RomainSauvaire opened this issue Feb 3, 2014 · 3 comments

Comments

@RomainSauvaire
Copy link

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

@anlutro
Copy link
Contributor

anlutro commented Feb 6, 2014

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.

@RomainSauvaire
Copy link
Author

Thank you @anlutro for this explanation I was not able to figure out myself.

@bgaeddert
Copy link

Thanks @anlutro, your explanation was a big help to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants