latest('published_at')->paginate(10); return view('posts.index', compact('posts')); } public function show(Post $post) { if ($post->status !== 'published') { abort(404); } $comments = $post->comments()->approved()->latest()->get(); return view('posts.show', compact('post', 'comments')); } public function create() { return view('posts.create'); } public function store(Request $request) { $validated = $request->validate([ 'title' => 'required|max:255', 'content' => 'required', 'status' => 'required|in:draft,published,scheduled', 'scheduled_at' => 'nullable|date|after:now', ]); $post = Post::create($validated); if ($validated['status'] === 'published') { $post->publish(); } elseif ($validated['status'] === 'scheduled' && $validated['scheduled_at']) { $post->schedule($validated['scheduled_at']); } return redirect()->route('home', $post) ->with('success', 'Пост успешно создан'); } public function edit(Post $post) { return view('posts.edit', compact('post')); } public function update(Request $request, Post $post) { $validated = $request->validate([ 'title' => 'required|max:255', 'content' => 'required', 'status' => 'required|in:draft,published,scheduled', 'scheduled_at' => 'nullable|date|after:now', ]); $post->update($validated); if ($validated['status'] === 'published' && $post->status !== 'published') { $post->publish(); } elseif ($validated['status'] === 'scheduled' && $validated['scheduled_at']) { $post->schedule($validated['scheduled_at']); } return redirect()->route('home', $post) ->with('success', 'Пост обновлен'); } public function destroy(Post $post) { $post->delete(); return redirect()->route('home') ->with('success', 'Пост удален'); } }