ChatGptApiSetting::query()->latest('id')->get(), ]); } /** * Store a new encrypted OpenAI API key setting. */ public function store(StoreChatGptApiSettingRequest $request): RedirectResponse { $data = $request->validated(); ChatGptApiSetting::query()->update(['is_active' => false]); ChatGptApiSetting::query()->create([ 'name' => $data['name'] ?: 'OpenAI API Key', 'api_key' => $data['api_key'], 'is_active' => (bool) ($data['is_active'] ?? true), ]); return redirect() ->route('chatgpt.index') ->with('status', 'OpenAI API key saved.'); } /** * Update an encrypted OpenAI API key setting. */ public function update(UpdateChatGptApiSettingRequest $request, ChatGptApiSetting $setting): RedirectResponse { $data = $request->validated(); if ((bool) ($data['is_active'] ?? false)) { ChatGptApiSetting::query() ->whereKeyNot($setting->getKey()) ->update(['is_active' => false]); } $setting->name = $data['name'] ?: $setting->name; $setting->is_active = (bool) ($data['is_active'] ?? false); if (! empty($data['api_key'])) { $setting->api_key = $data['api_key']; } $setting->save(); return redirect() ->route('chatgpt.index') ->with('status', 'OpenAI API key updated.'); } /** * Delete an encrypted OpenAI API key setting. */ public function destroy(ChatGptApiSetting $setting): RedirectResponse { $setting->delete(); return redirect() ->route('chatgpt.index') ->with('status', 'OpenAI API key deleted.'); } }