orderByDesc('created_at')->get(); return view('admin.reviews.index', compact('reviews')); } // Форма создания нового отзыва public function create(): View { return view('admin.reviews.form', ['review' => new Review]); } // Сохранение нового отзыва public function store(Request $request): RedirectResponse { $data = $this->validated($request); Review::create($data); $this->flushCache(); return redirect()->route('admin.reviews.index')->with('success', 'Отзыв добавлен.'); } // Форма редактирования отзыва public function edit(Review $review): View { return view('admin.reviews.form', compact('review')); } // Обновление отзыва public function update(Request $request, Review $review): RedirectResponse { $review->update($this->validated($request)); $this->flushCache(); return redirect()->route('admin.reviews.index')->with('success', 'Отзыв обновлён.'); } // Удаление отзыва public function destroy(Review $review): RedirectResponse { $review->delete(); $this->flushCache(); return redirect()->route('admin.reviews.index')->with('success', 'Отзыв удалён.'); } // Переключение видимости (активен / скрыт) без перехода на форму public function toggle(Review $review): RedirectResponse { $review->update(['is_active' => ! $review->is_active]); $this->flushCache(); return back()->with('success', $review->is_active ? 'Отзыв активирован.' : 'Отзыв скрыт.'); } // ── Приватные вспомогательные ──────────────────────────────────────── // Валидация и подготовка данных из запроса private function validated(Request $request): array { $request->validate([ 'author' => 'required|string|max:128', 'car_name' => 'nullable|string|max:128', 'rating' => 'required|integer|min:1|max:5', 'body' => 'required|string|max:2000', 'review_date' => 'nullable|date', 'is_active' => 'nullable|boolean', 'sort_order' => 'nullable|integer|min:0', ]); return [ 'author' => $request->input('author'), 'car_name' => $request->input('car_name') ?: null, 'rating' => (int) $request->input('rating', 5), 'body' => $request->input('body'), 'review_date' => $request->input('review_date') ?: null, 'is_active' => $request->boolean('is_active'), 'sort_order' => (int) $request->input('sort_order', 0), ]; } // Сбрасываем кеш страниц при изменении отзывов private function flushCache(): void { foreach (['home', 'services', 'contacts', 'privacy', 'offer'] as $slug) { Cache::forget('page_sections.'.$slug); Cache::forget('page.'.$slug); } } }