get(["user_id" => $user_id, 'order by' => ['sort' => 'asc']], ['id', 'class_name', 'is_default']); if (empty($items)) { return []; } $rows = Article::select(['class_id', Db::raw('count(class_id) as count')])->where('user_id', $user_id)->where('status', 1)->groupBy(['class_id'])->get()->keyBy("class_id")->toArray(); foreach ($items as $k => $val) { $items[$k]['count'] = isset($rows[$val['id']]) ? $rows[$val['id']]['count'] : 0; } return $items; } }