How to optimize a function? [closed]

  eloquent, laravel, php

All the best, I’m a little LOL?. I am only learning laravel and cannot understand how this code can be optimized

$items = DB::table('menu_items as i')
    ->selectRaw('i.*, COUNT(childs.id) as childs_count')
    ->leftJoin('menu_items as childs', function($join) {
        $join->on(DB::raw('childs.parent_id'), '=', DB::raw('i.id'))
            ->whereRaw('childs.is_enabled = 1');
    });

    if($menu_id !== false){
        $items->where('i.menu_id', $menu_id);
    }

    if ($parent_id !== false){
        $items->where('i.parent_id', $parent_id);
    }

    $items->groupBy('i.id')
        ->orderBy('i.ordering', 'asc');

return $items->get()->toArray();

Source: Ask PHP

LEAVE A COMMENT