route, 'module',true); $data['breadcrumbs'] = [ ['name' => 'Dashboard'], ['name' => 'Pengaturan'], ['name' => 'Unit Konversi','active' => true], ]; $data['title'] = $this->title; $data['route'] = $this->route; $data['kategori'] = Kategori::all(); $data['unit'] = Unit::where('kategori_id',decode_id(@request()->kategori_id))->get(); return view($this->template.'.index',$data); } public function grid(Request $request) { if(@$request->kategori_id){ $data = Unit::where('kategori_id',decode_id($request->kategori_id))->orderBy('UnitId','ASC')->get(); }else{ $data = []; } $_data = []; foreach ($data as $key => $row) { //unit to foreach ($data as $keyAr => $rowAr) { $_ardata['input_'.$rowAr->UnitId.'_'.strtolower($rowAr->kode)] = ''; } $action = ''; $status = ''; if($row->status == 0){ $status = ' Tidak Aktif '; }else{ $status = ' Aktif '; } $unit = Unit::where('kode',$row->kode)->first(); // $input = '
'; // $input = ''; // $input .= ''; $action .= ''; $_data[] = array_merge([ 'no' => $key+1, 'id' => encode_id($row->UnitId), 'kategori' => @$row->kategori->nama, 'nama' => @$row->nama, 'kategoriID' => @$row->kategori->KategoriId, 'UnitId' => $unit->UnitId, 'status' => @$status, 'input' => @$input, 'action' => @$action, ],$_ardata); } // return response()->json($_data); // Return the data as a JSON response return response()->json($_data); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $kategoriId = decode_id($request->kategori_id); foreach ($request->all() as $key => $value) { // pastikan key diawali "val_" if (!str_starts_with($key, 'val_')) { continue; } // pecah: "val_6_17" -> ["val", "6", "17"] $parts = explode('_', $key); // jaga-jaga kalau format gak sesuai if (count($parts) !== 3) { continue; } $fromId = (int) $parts[1]; $toId = (int) $parts[2]; // skip kalau value kosong (opsional) if ($value === null || $value === '') { continue; } UnitKonversi::updateOrCreate( ['kategori_id' => $kategoriId, 'from_id' => $fromId, 'to_id' => $toId], ['value' => $value, 'updated_at' => now()] ); } return redirect()->back()->with([ 'message' => 'Berhasil update data', 'type' => 'success', ]); } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit(string $id) { // } /** * Update the specified resource in storage. */ public function update($id = null) { $data['breadcrumbs'] = [ ['name' => 'Dashboard'], ['name' => 'Pengaturan'], ['name' => 'Unit Konversi','active' => true], ]; $keyId = decode_id($id); $data['title'] = $this->title; $data['route'] = $this->route; $data['keyId'] = $id; $data['item'] = UnitKonversi::where('UnitKonversiId',$keyId)->first(); $data['kategori'] = Kategori::all(); return view($this->template.'.form',$data); } public function delete($id) { $keyId = decode_id($id); $data = UnitKonversi::where('UnitKonversiId',$keyId)->delete(); return response()->json(['success' => true,'message' => 'Berhasil update data','type' => 'success']); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } }