I had similar issue and although time have passed since it may be useful to someone today.
The Problem I had was that when I called unique
method on collection of items it didn't worked, that is probably the reason the first answer got accepted. So if you have models and you want to remove duplicates based on a specific field you can pass parameter to your unique
method, in this case it would be:
$countries->unique('code');
that way you'll only have countries with unique codes. You may notice that only the first value stays, so if you develop a shopping cart application and want for some reason merge carts and only want to have recent items you can just reverse the collection and call unique
and reverse it back:
$countries->reverse()->unique('code')->reverse(); // it doesn't really make sense in this example though
it is probably not the best option and it is better to do filtering on the database side but it is nice to have options.
->groupby('country')
or->distinct()->get(array('country'))
in your query? – Marathon