App\Http\Controllers\FreeProductsController::getParentAndChildren PHP Method

getParentAndChildren() public static method

build the free prodcuts tree where parent will be a free product and children are the products list associated.
public static getParentAndChildren ( [array] $select = ['id', 'description'], [integer] $limit = 5 )
$select [array]
$limit [integer]
    public static function getParentAndChildren($select = ['id', 'description'], $limit = 5)
    {
        $events = FreeProduct::select($select)->where('status', '1')->with('orders')->get()->take($limit);
        $list = [];
        $events->each(function ($event) use(&$list, $select) {
            foreach ($select as $value) {
                $list[$event->id][$value] = $event->{$value};
            }
            $products = FreeProduct::find($event->id)->products->take(6)->toArray();
            if ($products) {
                $list[$event->id]['products'] = $products;
            }
            unset($products);
        });
        return $list;
    }