<?php
namespace App\Http\Controllers;
use App\Models\Kecamatan;
use App\Models\KabupatenKota;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class KecamatanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$q = request()->query('q');
$datas = Kecamatan::with('kabupaten_kota')->where('nama','like','%'.$q.'%')->paginate(10)->appends(['q'=>$q]);
return view('kecamatan.index',compact('datas','q'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$kabupatenKotas = KabupatenKota::all();
return view('kecamatan.create',compact('kabupatenKotas'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
'nama' => 'required|unique:kabupaten_kotas',
'provinsi_id' => 'required|exists:kabupatenKotas,id'
]);
DB::beginTransaction();
try {
Kecamatan::create($request->all());
DB::commit();
return redirect()->route('master-data.kecamatan.index')->with('success','Data berhasil disimpan');
} catch (\Throwable $th) {
Log::error($th->getMessage());
DB::rollback();
return redirect()->route('master-data.kecamatan.index')->with('error','Data gagal disimpan');
}
}
/**
* Display the specified resource.
*/
public function show(Kecamatan $kecamatan)
{
return view('kecamatan.show',compact('kecamatan'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Kecamatan $kecamatan)
{
$kabupatenKotas = KabupatenKota::all();
return view('kecamatan.edit',['data'=>$kecamatan,'kabupatenKotas'=>$kabupatenKotas]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Kecamatan $kecamatan)
{
$request->validate([
'nama' => 'required|unique:kabupaten_kotas,nama,'.$kecamatan->id,
'provinsi_id' => 'required|exists:kabupatenKotas,id'
]);
DB::beginTransaction();
try {
$kecamatan->update($request->all());
DB::commit();
return redirect()->route('master-data.kecamatan.index')->with('success','Data berhasil diubah');
} catch (\Throwable $th) {
Log::error($th->getMessage());
DB::rollback();
return redirect()->route('master-data.kecamatan.index')->with('error','Data gagal diubah');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Kecamatan $kecamatan)
{
DB::beginTransaction();
try {
$kecamatan->delete();
DB::commit();
return redirect()->route('master-data.kecamatan.index')->with('success','Data berhasil dihapus');
} catch (\Throwable $th) {
Log::error($th->getMessage());
DB::rollback();
return redirect()->route('master-data.kecamatan.index')->with('error','Data gagal dihapus');
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]