get(); return view('admin.banners.index', compact('banners')); } /** * Show the form for creating a new resource. */ public function create() { return view('admin.banners.create'); } /** * Store a newly created resource in storage. */ public function store(Request $request) { $request->validate([ 'judul' => 'required|string|max:255', 'deskripsi' => 'nullable|string', 'gambar' => 'required|image|max:2048', 'url' => 'nullable|string|max:255', 'aktif' => 'required|boolean', 'tanggal_mulai' => 'nullable|date', 'tanggal_selesai' => 'nullable|date|after_or_equal:tanggal_mulai', ]); $data = $request->all(); // Otomatisasi urutan $lastBanner = Banner::orderBy('urutan', 'desc')->first(); $data['urutan'] = $lastBanner ? $lastBanner->urutan + 1 : 1; // Menangani tanggal kosong if (empty($data['tanggal_mulai'])) { $data['tanggal_mulai'] = null; } if (empty($data['tanggal_selesai'])) { $data['tanggal_selesai'] = null; } if ($request->hasFile('gambar')) { $data['gambar'] = $request->file('gambar')->store('banners', 'public'); } Banner::create($data); return redirect()->route('admin.banners.index')->with('success', 'Banner berhasil ditambahkan'); } /** * Display the specified resource. */ public function show(Banner $banner) { return view('admin.banners.show', compact('banner')); } /** * Show the form for editing the specified resource. */ public function edit(Banner $banner) { return view('admin.banners.edit', compact('banner')); } /** * Update the specified resource in storage. */ public function update(Request $request, Banner $banner) { $request->validate([ 'judul' => 'required|string|max:255', 'deskripsi' => 'nullable|string', 'gambar' => 'nullable|image|max:2048', 'url' => 'nullable|string|max:255', 'urutan' => 'nullable|integer|min:1', 'tanggal_mulai' => 'nullable|date', 'tanggal_selesai' => 'nullable|date|after_or_equal:tanggal_mulai', 'aktif' => 'required|boolean', ]); $data = $request->all(); // Menangani tanggal kosong if (empty($data['tanggal_mulai'])) { $data['tanggal_mulai'] = null; } if (empty($data['tanggal_selesai'])) { $data['tanggal_selesai'] = null; } if ($request->hasFile('gambar')) { // Hapus gambar lama jika ada if ($banner->gambar) { Storage::disk('public')->delete($banner->gambar); } $data['gambar'] = $request->file('gambar')->store('banners', 'public'); } $banner->update($data); return redirect()->route('admin.banners.index')->with('success', 'Banner berhasil diperbarui'); } /** * Remove the specified resource from storage. */ public function destroy(Banner $banner) { // Hapus gambar jika ada if ($banner->gambar) { Storage::disk('public')->delete($banner->gambar); } $banner->delete(); return redirect()->route('admin.banners.index')->with('success', 'Banner berhasil dihapus'); } /** * Toggle status banner */ public function toggleStatus(Banner $banner) { $banner->aktif = !$banner->aktif; $banner->save(); return redirect()->route('admin.banners.index')->with('success', 'Status banner berhasil diubah'); } /** * Move banner up in order */ public function moveUp(Banner $banner) { $prevBanner = Banner::where('urutan', '<', $banner->urutan) ->orderBy('urutan', 'desc') ->first(); if ($prevBanner) { // Swap positions $tempUrutan = $prevBanner->urutan; $prevBanner->urutan = $banner->urutan; $banner->urutan = $tempUrutan; $prevBanner->save(); $banner->save(); return redirect()->route('admin.banners.index')->with('success', 'Urutan banner berhasil diubah'); } return redirect()->route('admin.banners.index')->with('info', 'Banner sudah berada di urutan teratas'); } /** * Move banner down in order */ public function moveDown(Banner $banner) { $nextBanner = Banner::where('urutan', '>', $banner->urutan) ->orderBy('urutan', 'asc') ->first(); if ($nextBanner) { // Swap positions $tempUrutan = $nextBanner->urutan; $nextBanner->urutan = $banner->urutan; $banner->urutan = $tempUrutan; $nextBanner->save(); $banner->save(); return redirect()->route('admin.banners.index')->with('success', 'Urutan banner berhasil diubah'); } return redirect()->route('admin.banners.index')->with('info', 'Banner sudah berada di urutan terbawah'); } }