Update fitur update Curas dan Curanmor
This commit is contained in:
parent
319f3ca1ff
commit
48850420c1
|
@ -6,6 +6,7 @@
|
|||
use App\Models\Curanmor;
|
||||
use App\Models\Kecamatan;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CuranmorController extends Controller
|
||||
{
|
||||
|
@ -77,7 +78,30 @@ public function edit($curanmor)
|
|||
*/
|
||||
public function update(Request $request, Curanmor $curanmor)
|
||||
{
|
||||
//
|
||||
try {
|
||||
|
||||
// Validasi input
|
||||
$request->validate([
|
||||
'kecamatan_id' => [
|
||||
'required',
|
||||
'exists:kecamatans,id',
|
||||
Rule::unique('curanmors')->ignore($curanmor->id),
|
||||
],
|
||||
'klaster_id' => 'required|exists:klasters,id',
|
||||
'jumlah_curanmor' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
// Update data
|
||||
$curanmor->update([
|
||||
'kecamatan_id' => $request->kecamatan_id,
|
||||
'klaster_id' => $request->klaster_id,
|
||||
'jumlah_curanmor' => $request->jumlah_curanmor,
|
||||
]);
|
||||
|
||||
return redirect('/curanmor')->with('succes', 'Data Kecamatan Berhasil Diubah');
|
||||
} catch (\Exception $e) {
|
||||
return redirect('/curanmor')->with('error', 'Data Kecamatan Gagal Diubah: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,13 +110,21 @@ public function update(Request $request, Curanmor $curanmor)
|
|||
public function destroy($curanmor)
|
||||
{
|
||||
try {
|
||||
// Cari data berdasarkan ID
|
||||
$hapus = Curanmor::find($curanmor);
|
||||
Curanmor::destroy($hapus);
|
||||
return redirect('/curanmor')->with('succes', 'Data Curanmor Berhasil Di Hapus');
|
||||
|
||||
// Pastikan data ditemukan sebelum menghapus
|
||||
if (!$hapus) {
|
||||
return redirect('/curanmor')->with('error', 'Data tidak ditemukan.');
|
||||
}
|
||||
|
||||
// Hapus data
|
||||
$hapus->delete();
|
||||
|
||||
return redirect('/curanmor')->with('succes', 'Data Curanmor Berhasil Dihapus');
|
||||
} catch (\Exception $e) {
|
||||
return redirect('/curanmor')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return redirect('/curanmor')->with('error', 'Data Curanmor '. $curanmor->nama_kecamatan .' Gagal Di Hapus');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,25 +78,39 @@ public function edit($curas)
|
|||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Curas $curas)
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$validateData = $request->validate([
|
||||
'kecamatan_id' => 'sometimes|required|max:255' . $curas->kecamatan_id,
|
||||
'jumlah_curas' => 'sometimes|required|integer',
|
||||
'klaster_id' => 'sometimes|required|max:255' . $curas->klaster_id,
|
||||
// Cari data berdasarkan ID yang dikirim
|
||||
$curas = Curas::findOrFail($id);
|
||||
|
||||
// Debugging untuk memastikan data ditemukan
|
||||
// dd($curas->toArray()); // Jika berhasil, ini akan menampilkan data curas
|
||||
|
||||
// Validasi input
|
||||
$request->validate([
|
||||
'kecamatan_id' => [
|
||||
'required',
|
||||
'exists:kecamatans,id',
|
||||
Rule::unique('curas')->ignore($curas->id),
|
||||
],
|
||||
'klaster_id' => 'required|exists:klasters,id',
|
||||
'jumlah_curas' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
// Pastikan hanya field yang diisi yang akan diperbarui
|
||||
$curas->update(array_filter($validateData));
|
||||
// Update data
|
||||
$curas->update([
|
||||
'kecamatan_id' => $request->kecamatan_id,
|
||||
'klaster_id' => $request->klaster_id,
|
||||
'jumlah_curas' => $request->jumlah_curas,
|
||||
]);
|
||||
|
||||
return redirect('/curas')->with('succes', 'Data Kecamatan Berhasil Diubah');
|
||||
} catch (\Exception $e) {
|
||||
return redirect('/curas')->with('error', 'Data Kecamatan Gagal Diubah: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return redirect('/curas')->with('error', 'Data Kecamatan Gagal Diubah');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -107,14 +121,22 @@ public function update(Request $request, Curas $curas)
|
|||
public function destroy($curas)
|
||||
{
|
||||
try {
|
||||
// Cari data berdasarkan ID
|
||||
$hapus = Curas::find($curas);
|
||||
Curas::destroy($hapus);
|
||||
return redirect('/curas')->with('succes', 'Data Curas Berhasil Di Hapus');
|
||||
|
||||
// Pastikan data ditemukan sebelum menghapus
|
||||
if (!$hapus) {
|
||||
return redirect('/curas')->with('error', 'Data tidak ditemukan.');
|
||||
}
|
||||
|
||||
// Hapus data
|
||||
$hapus->delete();
|
||||
|
||||
return redirect('/curas')->with('succes', 'Data Curas Berhasil Dihapus');
|
||||
} catch (\Exception $e) {
|
||||
return redirect('/curas')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return dd($e);
|
||||
// redirect('/curas')->with('error', 'Data Curas '. $curas->nama_kecamatan .' Gagal Di Hapus | Hapus Data Curas Atau Curanmor Untuk Klaster '. $curas->nama_kecamatan.' Terlebih Dahulu');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,16 +18,13 @@
|
|||
<div class="col-md-12">
|
||||
<div class="form-group">
|
||||
<label>Nama Kecamatan *</label>
|
||||
<select class="selectpicker form-control" data-style="py-0" id="kecamatan_id" name="kecamatan_id">
|
||||
<option value="" selected disabled> Pilih Kecamatan </option>
|
||||
@foreach ( $kecamatans as $kecamatan )
|
||||
<option value="{{ $kecamatan->id }}"
|
||||
{{ old('kecamatan_id', $curanmor->kecamatan_id) == $kecamatan->id ? 'selected' : '' }}>
|
||||
{{ $kecamatan->nama_kecamatan }}
|
||||
</option>
|
||||
@endforeach
|
||||
<input type="text" class="form-control" placeholder="Nama Kecamatan"
|
||||
id="nama_kecamatan" name="nama_kecamatan"
|
||||
value="{{ $curanmor->punyaKecamatanCuranmor->nama_kecamatan }}"
|
||||
readonly>
|
||||
|
||||
</select>
|
||||
<!-- Input hidden untuk mengirim ID kecamatan -->
|
||||
<input type="hidden" name="kecamatan_id" value="{{ $curanmor->kecamatan_id }}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
|
|
|
@ -17,7 +17,18 @@
|
|||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="form-group">
|
||||
|
||||
<label>Nama Kecamatan *</label>
|
||||
<input type="text" class="form-control" placeholder="Nama Kecamatan"
|
||||
id="nama_kecamatan" name="nama_kecamatan"
|
||||
value="{{ $curas->punyaKecamatanCuras->nama_kecamatan }}"
|
||||
readonly>
|
||||
|
||||
<!-- Input hidden untuk mengirim ID kecamatan -->
|
||||
<input type="hidden" name="kecamatan_id" value="{{ $curas->kecamatan_id }}">
|
||||
|
||||
|
||||
{{-- <label>Nama Kecamatan *</label>
|
||||
<select class="selectpicker form-control" data-style="py-0" id="kecamatan_id" name="kecamatan_id">
|
||||
<option value="" selected disabled> Pilih Kecamatan </option>
|
||||
@foreach ( $kecamatans as $kecamatan )
|
||||
|
@ -27,7 +38,7 @@
|
|||
</option>
|
||||
@endforeach
|
||||
|
||||
</select>
|
||||
</select> --}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
|
@ -49,7 +60,7 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary mr-2">Tambah Data Kasus Curas</button>
|
||||
<button type="submit" class="btn btn-primary mr-2">Ubah Data Kasus Curas</button>
|
||||
<button type="reset" class="btn btn-danger">Reset</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue