관리-도구
편집 파일: TestimonialsController.php
<?php namespace App\Http\Controllers; use App\Models\Testimonials; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; class TestimonialsController extends Controller { public function index() { $testimonials = Testimonials::orderBy('created_at', 'desc')->get(); return view('admin.testimonials', compact('testimonials')); } public function store(Request $request) { try { // Validate the data $validateData = $request->validate([ 'name' => 'required|string', 'designation' => 'required|string', 'review' => 'required|string', 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg', ]); if (!$request->hasFile('image')) { return redirect()->back()->withErrors(['image' => 'No file uploaded']); } $imagePath = $request->file('image')->store('slider', 'public'); // Create new instance for testimonials $testimonials = new Testimonials(); $testimonials->name = $validateData['name']; $testimonials->location = $validateData['designation']; $testimonials->comment = $validateData['review']; $testimonials->image = $imagePath; // Handle the image upload // Save the testimonial record $testimonials->save(); return redirect()->route('admin-testimonials.index')->with('success', 'New testimonial added successfully'); } catch (\Exception $e) { Log::error('An error occurred while inserting testimonial: ' . $e->getMessage()); return redirect()->back()->with('error', 'Failed to add testimonial.'); } } public function update(Request $request, $id) { $validatedData = $request->validate([ 'name' => 'required|string', 'designation' => 'required|string', 'review' => 'required|string', 'image' => 'nullable|file|image', ]); $testimonial = Testimonials::findOrFail($id); $testimonial->name = $validatedData['name']; $testimonial->location = $validatedData['designation']; $testimonial->comment = $validatedData['review']; if ($request->hasFile('image')) { $imagePath = $request->file('image')->store('uploads/services', 'public'); if ($testimonial->image) { Storage::disk('public')->delete($testimonial->image); } $testimonial->image = $imagePath; } $testimonial->save(); return redirect()->route('admin-testimonials.index')->with('success', 'Testimonial updated successfully.'); } public function destroy($id) { $testimonial = Testimonials::findOrFail($id); $testimonial->delete(); return redirect()->route('admin-testimonials.index')->with('Testimonial deleted successfully'); } }