user_id === auth()->id(), 403); return view('organizations.show', compact('organization')); } public function store(Request $request): JsonResponse|RedirectResponse { $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], ]); $user = Auth::user(); $org = $user->organizations()->create(['name' => $validated['name']]); $user->update([ 'has_organization' => true, 'current_organization_id' => $org->id, ]); return response()->json([ 'redirect' => route('dashboard'), 'message' => 'Organization created successfully.', ]); } public function switch(Organization $organization) { abort_unless($organization->user_id === auth()->id(), 403); auth()->user()->update(['current_organization_id' => $organization->id]); return redirect()->route('organizations.show', $organization); } public function destroy(Organization $organization): JsonResponse|RedirectResponse { $user = Auth::user(); if ($organization->user_id !== $user->id) { return response()->json(['error' => 'Unauthorized'], 403); } if ($user->organizations()->count() <= 1) { return response()->json([ 'error' => 'You must have at least one organization.', ]); } $organization->delete(); return response()->json([ 'message' => 'Organization deleted.', ]); } }