98 lines
3.2 KiB
PHP
98 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ProfilDesa;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ProfilDesaController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
// Mengambil semua data profil desa (biasanya hanya ada 1)
|
|
$profils = ProfilDesa::all();
|
|
return view('profil_desa.index', compact('profils'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
// PENGAMAN: Jika profil desa sudah ada, arahkan langsung ke halaman Edit.
|
|
// Desa Bendelan tidak perlu punya 2 profil ganda.
|
|
$existingProfil = ProfilDesa::first();
|
|
if ($existingProfil) {
|
|
return redirect()->route('profil_desa.edit', $existingProfil->id)
|
|
->with('info', 'Profil desa sudah dibuat. Silakan perbarui data di sini.');
|
|
}
|
|
|
|
return view('profil_desa.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'sejarah_singkat' => 'nullable|string',
|
|
'visi' => 'nullable|string',
|
|
'misi' => 'nullable|string',
|
|
'nama_kepala_desa' => 'nullable|string|max:255',
|
|
'foto_kepala_desa' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
|
]);
|
|
|
|
$data = $request->except('foto_kepala_desa');
|
|
|
|
if ($request->hasFile('foto_kepala_desa')) {
|
|
$data['foto_kepala_desa'] = $request->file('foto_kepala_desa')->store('profil_desa', 'public');
|
|
}
|
|
|
|
ProfilDesa::create($data);
|
|
|
|
return redirect()->route('profil_desa.index')->with('success', 'Profil Desa Bendelan berhasil disimpan!');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$profil = ProfilDesa::findOrFail($id);
|
|
return view('profil_desa.edit', compact('profil'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$profil = ProfilDesa::findOrFail($id);
|
|
|
|
$request->validate([
|
|
'sejarah_singkat' => 'nullable|string',
|
|
'visi' => 'nullable|string',
|
|
'misi' => 'nullable|string',
|
|
'nama_kepala_desa' => 'nullable|string|max:255',
|
|
'foto_kepala_desa' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
|
]);
|
|
|
|
$data = $request->except('foto_kepala_desa');
|
|
|
|
// Jika ada update foto kepala desa
|
|
if ($request->hasFile('foto_kepala_desa')) {
|
|
if ($profil->foto_kepala_desa && Storage::disk('public')->exists($profil->foto_kepala_desa)) {
|
|
Storage::disk('public')->delete($profil->foto_kepala_desa);
|
|
}
|
|
$data['foto_kepala_desa'] = $request->file('foto_kepala_desa')->store('profil_desa', 'public');
|
|
}
|
|
|
|
$profil->update($data);
|
|
|
|
return redirect()->route('profil_desa.index')->with('success', 'Profil Desa Bendelan berhasil diperbarui!');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$profil = ProfilDesa::findOrFail($id);
|
|
|
|
if ($profil->foto_kepala_desa && Storage::disk('public')->exists($profil->foto_kepala_desa)) {
|
|
Storage::disk('public')->delete($profil->foto_kepala_desa);
|
|
}
|
|
|
|
$profil->delete();
|
|
|
|
return redirect()->route('profil_desa.index')->with('success', 'Profil Desa berhasil dihapus!');
|
|
}
|
|
}
|