Friday, April 14, 2017

Laravel Collection filter into Parent Data

I have created map table for relation between Products & Product Categories. I want to get Product object from collection using below code.

return  $collection->products->product_categories()->get()->filter(function($item) use ($itemIds)
                {
                    if(isset($item->pivot->parent->id))
                    {   
                        return $item->pivot->parent;    
                    }

                });

If I dump and die parent then I can see Product object but when I try to return Product it shows Category data.



via Anuj Jaha

Advertisement