| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use Illuminate\Support\Str;
- use App\Models\Post;
- class PostController extends Controller
- {
- /**
- * Display a listing of the resource.
- */
- public function index()
- {
- //
- $posts = Post::latest()->paginate(10);
- return view('admin.posts.index', compact('posts'));
- }
- /**
- * Show the form for creating a new resource.
- */
- public function create()
- {
- //
- return view('admin.posts.form');
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(Request $request)
- {
- //
- $validated = $request->validate([
- 'title' => 'required|string',
- 'content' => 'required|string',
- ]);
- $post = Post::create([
- 'title' => $validated['title'],
- 'slug' => Str::slug($validated['title']),
- 'content' => $validated['content'],
- 'is_published' => $request->boolean('is_published'),
- 'published_at' => $request->boolean('is_published') ? now() : null,
- 'user_id' => auth()->id(),
- 'scheduled_at' => $request->filled('scheduled_at') ? $request->input('scheduled_at') : null,
- ]);
- return redirect()->route('admin.posts.index')->with('success', 'Пост успешно создан!');
- }
- /**
- * Display the specified resource.
- */
- public function show(string $id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- */
- public function edit(Post $post)
- {
- //
- return view('admin.posts.form', compact('post'));
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(Request $request, Post $post)
- {
- //
- $validated = $request->validate([
- 'title' => 'required|string',
- 'content' => 'required|string',
- ]);
- $post->update([
- 'title' => $validated['title'],
- 'content' => $validated['content'],
- 'is_published' => $request->boolean('is_published'),
- 'published_at' => $request->boolean('is_published') ? now() : null,
- 'scheduled_at' => $request->filled('scheduled_at') ? $request->input('scheduled_at') : null,
- ]);
- return redirect()->route('admin.posts.index')->with('success', 'Пост успешно изменен!');
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(Post $post)
- {
- //
- $post->delete();
- return redirect()->route('admin.posts.index')->with('success', 'Пост успешно удален!');
- }
- }
|