How to fetch unique values for a single column in Laravel 5.2?
<p>In my <code>products</code> table, each <code>Product</code> has a column called <code>retailer</code>. In my view, I am trying to fetch and display all <em>unique</em> values for that <code>retailer</code> column from the table. Note that I only need distinct values for ONE single column, i.e. <code>retailer</code>. Here's my attempt:</p>

<pre><code>@foreach(\App\Product::select('retailer')-&gt;groupBy('retailer')-&gt;get() as $retailer)
    {{$retailer-&gt;retailer}}
@endforeach
</code></pre>

<p><a href="https://stackoverflow.com/questions/25228823/laravel-get-distinct-values-for-non-key-column-fields">Here</a> I've read that <code>groupBy('retailers)</code> would get the unique values. That indeed worked, but since I am passing an array <code>$products</code> to the view, which contains all products, I thought looping through <code>$products</code> would be more efficient:</p>

<pre><code> @foreach($products-&gt;select('retailer')-&gt;unique() as $retailer)
     {{$retailer}}
 @endforeach
</code></pre>

<p>but that didn't work.</p>

<p><strong>Q</strong>: What am I doing wrong here? Is there a more efficient way to go about this? Is it better to resort to Query Builder (rather than Eloquent) in this case?</p>

<p>Thank you!</p>