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 does not work without an overridden getKey() method #7

Open
Cederman opened this issue Jan 25, 2016 · 2 comments
Open

Comments

@Cederman
Copy link

I realised that if you want to merge two collections like this:

$collection1->merge($collection2)

You will get an exception saying that you haven't implemented a getKey() method. After som digging around, I found that, as long as you implement this method on the model and return something unique for each model instance, the merge will work.

I guess my tip is that the getKey() method is implemented in the Model.php file and that the developers using this package can provide an id attribute which is unique for all model instances :)

@jenssegers
Copy link
Owner

This is pretty Laravel specific of course. Indeed, if you want it to be compatible with the collections, the method should be there.

@Cederman
Copy link
Author

True indeed. But Collections is a great way to work with models and simplify things so I figured I might as well share :)

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

2 participants