관리-도구
편집 파일: TeamController.php
<?php namespace App\Http\Controllers; use App\Models\Team; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; class TeamController extends Controller { public function index(){ $gallery = Team::all(); return view('admin.team', compact('gallery')); } public function store(Request $request){ try{ // check if file present in the request if(!$request->hasFile('image')){ return redirect()->back()->with('No file uploaded'); } // store image $imagePath = $request-> file('image')->store('gallery', 'public'); // create new instance $gallery = new Team(); $gallery -> image = $imagePath; $gallery -> save(); return redirect()->route('admin-team.index')-> with('Image added successfully'); } catch(\Exception $e){ Log::error('Image uploadng error:'. $e->getMessage()); } } public function update(Request $request, $id){ try{ $gallery = Team::findOrFail($id); if($request->hasFile('image')){ $imagePath = $request->file('image')->store('gallery', 'public'); if($gallery->image){ Storage::disk('public')->delete($gallery->image); } $gallery -> image = $imagePath; } $gallery-> save(); return redirect()->route('admin-team.index')->with('Image updated successfully'); } catch(\Exception $e){ Log::error('Image update error:'. $e->getMessage()); } } public function destroy($id){ $gallery = Team::findOrFail($id); $gallery -> delete(); return redirect()-> route('admin-team.index')->with('Image deleted successfully'); } }