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