orderByDesc('id') ->withCount('users') ->get() ->transform(function ($group) { $group->server_count = $group->servers()->count(); return $group; }); return $this->success($serverGroups); } public function save(Request $request) { if (empty($request->input('name'))) { return $this->fail([422, '组名不能为空']); } if ($request->input('id')) { $serverGroup = ServerGroup::find($request->input('id')); } else { $serverGroup = new ServerGroup(); } $serverGroup->name = $request->input('name'); return $this->success($serverGroup->save()); } public function drop(Request $request) { $groupId = $request->input('id'); $serverGroup = ServerGroup::find($groupId); if (!$serverGroup) { return $this->fail([400202, '组不存在']); } if (Server::whereJsonContains('group_ids', $groupId)->exists()) { return $this->fail([400, '该组已被节点所使用,无法删除']); } if (Plan::where('group_id', $groupId)->exists()) { return $this->fail([400, '该组已被订阅所使用,无法删除']); } if (User::where('group_id', $groupId)->exists()) { return $this->fail([400, '该组已被用户所使用,无法删除']); } return $this->success($serverGroup->delete()); } }