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