<?php
namespace App\Http\Controllers;
use App\Models\Kampung;
use App\Models\Kecamatan;
use Illuminate\Http\Request;
use Illuminate\Database\QueryException;
class KampungController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'SIP - Kampung';
$data['kampung'] = Kampung::all();
return view('Kampung.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'SIP - Tambah Kampung';
$data['kecamatan'] = Kecamatan::all();
return view('kampung.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
'nama' => 'required',
'kecamatan' => 'required',
]);
$data['nama'] = $request->nama;
$data['id_kecamatan'] = $request->kecamatan;
Kampung::create($data);
return redirect()->route('kampung.index')->with('success', 'Berhasil menambah data');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$data['title'] = 'SIP - Edit Kampung';
$data['kecamatan'] = Kecamatan::all();
$data['item'] = Kampung::where('id', $id)->first();
return view('kampung.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$request->validate([
'nama' => 'required',
'kecamatan' => 'required',
]);
$data['nama'] = $request->nama;
$data['id_kecamatan'] = $request->kecamatan;
Kampung::where('id', $id)->update($data);
return redirect()->route('kampung.index')->with('success', 'Berhasil mengubah data');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
try {
Kampung::where('id', $id)->delete();
return redirect()->route('kampung.index')->with('success', 'Berhasil menghapus data');
} catch (QueryException $e) {
return back()->with('error', 'Tidak dapat menghapus data yang berelasi!');
}
}
public function getKampungList($id)
{
$kampungs = Kampung::where('id_kecamatan', $id)->pluck('nama', 'id');
return response()->json($kampungs);
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]