Check if Object already exists in Collection - Laravel : <p>I am looking to add objects to a new collection as I loop through a series of different results.</p>

<p>The query:</p>

<pre><code>$osRed = Item::where('category', 'Hardware')
        -&gt;where(function ($query) {
            $query-&gt;where('operating_system', 'xxx')
                -&gt;orWhere('operating_system', 'xxx')
                -&gt;orWhere('operating_system', 'xxx');
        })
        -&gt;orderBy('operating_system', 'ASC')
        -&gt;get();
</code></pre>

<p>Then, loop through these results and add related objects to a new collection.</p>

<pre><code>foreach ($osRed as $os) {
        foreach ($os-&gt;services as $service) {
            if (!($servicesImpacted-&gt;contains($service))) {
                $servicesImpacted-&gt;push($service);
            }
        }
}
</code></pre>

<p>I then go on to another set of results and add the related services from those results to the collection.</p>

<p>However, it is not picking up that the object is already in the collection and I end up with a large number of (what appear to be) duplicates.  </p>

<p>Ideally, I would like to match up on the name attribute of the $service object, but that doesn't seem to be supported by the contains method.</p>

<pre><code>toString() must not throw an exception
</code></pre>
