authorize('viewAny', LegalDocument::class); $documents = LegalDocument::orderBy('type')->orderByDesc('version')->paginate(20); return view('legal.documents.index', compact('documents')); } /** * Show the form for editing a legal document. */ public function edit(string $type) { $document = LegalDocument::where('type', $type)->latest('version')->first(); if (!$document) { $document = new LegalDocument(['type' => $type]); } $this->authorize('update', $document); return view('legal.documents.edit', compact('document')); } /** * Update or create a legal document. */ public function update(Request $request, string $type) { $this->authorize('create', LegalDocument::class); $validated = $request->validate([ 'title' => ['required', 'string', 'max:255'], 'content' => ['required', 'string'], 'version' => ['required', 'string', 'max:50'], 'published_at' => ['nullable', 'date'], ]); $document = LegalDocument::where('type', $type)->latest('version')->first(); if ($document && $document->version === $validated['version']) { $document->update([ 'title' => $validated['title'], 'content' => $validated['content'], 'published_at' => $validated['published_at'] ? now() : null, ]); } else { $document = LegalDocument::create(array_merge($validated, [ 'type' => $type, 'published_by' => Auth::id(), 'published_at' => $validated['published_at'] ?? now(), ])); } return redirect()->route('legal.documents.index')->with('success', 'Documento atualizado com sucesso.'); } /** * Display the legal document page. */ public function show(string $type) { $document = LegalDocument::where('type', $type)->latest('version')->firstOrFail(); return view('legal.documents.show', compact('document')); } }