MIF_E31222541/app/Http/Controllers/Website/ParameterListHeightVillageC...

114 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\Website;
use App\Http\Controllers\Controller;
use App\Models\HeightVillage;
use App\Models\SubDistrict;
use App\Models\Village;
use Illuminate\Http\Request;
class ParameterListHeightVillageController extends Controller
{
public function index()
{
$listSubDistrict = SubDistrict::all();
$listHeightVillage = HeightVillage::all();
$years = array_unique(array_column($listHeightVillage->toArray(), 'year'));
sort($years);
return view('website.app.parameter-list-height-village', compact('listSubDistrict', 'years'));
}
public function getDataParameterListHeightVillage($year, $subDistrictId)
{
$listHeightVillage = HeightVillage::select(
'height_village.id as hv_id',
'height_village.village_id as hv_village_id',
'height_village.height',
'height_village.year',
'village.id as village_id',
'village.village',
'village.sub_district_id as village_sub_district_id',
'sub_district.id as sd_id',
'sub_district.sub_district'
)
->join('village', 'height_village.village_id', '=', 'village.id')
->join('sub_district', 'village.sub_district_id', '=', 'sub_district.id')
->where('village.sub_district_id', $subDistrictId)
->where('year', $year)->get();
return response()->json([
'listHeightVillage' => $listHeightVillage
]);
}
public function getVillage($id)
{
$village = Village::where('sub_district_id', $id)->get();
return response()->json([
'village' => $village
]);
}
public function addListHeightVillage(Request $request)
{
$subDistrictId = $request->input('subDistrictId');
$villageId = $request->input('villageId');
$year = $request->input('year');
$height = $request->input('height');
$heightByVillage = HeightVillage::where('village_id', '=', $villageId)
->where('year', '=', $year)
->get();
if (count($heightByVillage) > 0) {
toast('Gagal, Data ketingian tempat desa atau kelurahan (tahunan) sudah tersedia!', 'error');
return redirect()->back();
} else {
HeightVillage::create([
'id' => $year . $villageId,
'village_id' => $villageId,
'height' => $height,
'year' => $year,
]);
toast('Berhasil, Data ketingian tempat desa atau kelurahan (tahunan) disimpan!', 'success');
return redirect()->back();
}
}
public function updateListHeightVillage(Request $request)
{
$heightVillageId = $request->input('heightVillageId');
$subDistrictId = $request->input('subDistrictId');
$villageId = $request->input('villageId');
$year = $request->input('year');
$height = $request->input('height');
$heighVillageById = HeightVillage::where('id', $heightVillageId)
->where('village_id', $villageId)
->where('year', $year)
->first();
// dd($request->all(), $heighVillageById);
$heighVillageById['village_id'] = $villageId;
$heighVillageById['year'] = $year;
$heighVillageById['height'] = $height;
$heighVillageById->save();
toast('Berhasil, Data ketingian tempat desa atau kelurahan (tahunan) diubah!', 'success');
return redirect()->back();
}
public function deleteListHeightVillage($id)
{
$heightVillageById = HeightVillage::where('id', $id)->first();
$heightVillageById->delete();
toast('Berhasil, Data ketinggian tempat desa atau kelurahan (tahunan) dihapus', 'success');
return redirect()->back();
}
}