| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Post;
- use Illuminate\Http\Request;
- class PostController extends Controller
- {
- public function index()
- {
- $posts = Post::published()->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', 'Пост удален');
- }
- }
|