2 months ago

Get unique values of Eloquent relationship

Posted 2 months ago by stefan7

I have a Product and Branch model with m:n relationship. The following query gives me a collection of products, including the branches relationship. What is a good way to get a list of unique branches (used as a filter in a select list on frontend) with the following query?

  $products = Product::whereHas('categories', function($query){
                 $query->where('id', 2); 
            ->orderBy('created_at', 'desc')

Please sign in or create an account to participate in this conversation.