Wednesday, April 12, 2017

Laravel Product Brands

I have a category page.

$page = SubCategory::FindOrFail($id);
$products = $page->products;

I'm returning a view and showing all products related to the category.

I'm trying to create a filter and view all brands which are associated to the products returned in the category.

I could create a

$page = SubCategory::FindOrFail($id);
$products = $page->products;
$brands = array();

    foreach($products as $p){
                foreach($brands as $b){
                    if(!in_array($p->brand->id,$brands)){
                        $brands[] = $p->brand->id;
                    }
                }
    }

However I feel there could be a more eloquent way of doing it.



via Dev.Wol

Advertisement