From 79e43efa0194b4a902fc27868349d2d8475a891c Mon Sep 17 00:00:00 2001 From: FahrezaDaffa93 <117092951+FahrezaDaffa93@users.noreply.github.com> Date: Thu, 2 May 2024 10:23:46 +0700 Subject: [PATCH] fixing crud --- app/Http/Controllers/AturanController.php | 27 +++++- app/Http/Controllers/GejalaController.php | 36 +++++++- app/Http/Controllers/PenyakitController.php | 32 ++++++- app/Models/Penyakit.php | 3 + .../views/dashboard/Aturan/Aturan.blade.php | 24 +++--- .../dashboard/Aturan/edit-aturan.blade.php | 70 ++++++++++++++++ .../dashboard/gejala/add-gejala.blade.php | 11 ++- .../dashboard/gejala/edit-gejala.blade.php | 77 +++++++++++++++++ .../views/dashboard/gejala/gejala.blade.php | 38 +++++---- .../dashboard/penyakit/add-penyakit.blade.php | 6 +- .../penyakit/edit-penyakit.blade.php | 84 +++++++++++++++++++ .../dashboard/penyakit/penyakit.blade.php | 10 +-- routes/web.php | 32 +++---- 13 files changed, 384 insertions(+), 66 deletions(-) create mode 100644 resources/views/dashboard/Aturan/edit-aturan.blade.php create mode 100644 resources/views/dashboard/gejala/edit-gejala.blade.php create mode 100644 resources/views/dashboard/penyakit/edit-penyakit.blade.php diff --git a/app/Http/Controllers/AturanController.php b/app/Http/Controllers/AturanController.php index 65433cc..8a38ab4 100644 --- a/app/Http/Controllers/AturanController.php +++ b/app/Http/Controllers/AturanController.php @@ -35,7 +35,15 @@ public function create() */ public function store(Request $request) { - // + $validatedData = $request->validate([ + 'kode_penyakit' => 'required|string', + 'kode_gejala' => 'required|string', + + ]); + + Penyakit::create($validatedData); + + return redirect('penyakit')->with('success', 'Data gejala berhasil disimpan.'); } /** @@ -57,9 +65,22 @@ public function edit(string $id) /** * Update the specified resource in storage. */ - public function update(Request $request, string $id) + public function update(Request $request, string $id_aturan) { - // + $validatedData = $request->validate([ + 'kode_penyakit' => 'required|string|max:255', + 'kode_gejala' => 'required|string|max:255', // Foto produk menjadi opsional untuk diubah + ]); + + // Dapatkan data produk berdasarkan ID + $aturan = Aturan::findOrFail($id_aturan); + + // Perbarui data produk + $aturan->kode_penyakit = $validatedData['kode_penyakit']; + $aturan->kode_gejala = $validatedData['kode_gejala']; + + $aturan->save(); + return redirect()->route('aturan')->with('success', 'Produk berhasil diperbarui.'); } /** diff --git a/app/Http/Controllers/GejalaController.php b/app/Http/Controllers/GejalaController.php index 7ece0e5..14516b5 100644 --- a/app/Http/Controllers/GejalaController.php +++ b/app/Http/Controllers/GejalaController.php @@ -32,16 +32,29 @@ public function create() */ public function store(Request $request) { + try { $validatedData = $request->validate([ 'kode_gejala' => 'required|string', 'gejala' => 'required|string', 'nilai_densitas' => 'required|string', ]); - Gejala::create($validatedData); + // Buat objek Gejala baru + $gejala = new Gejala(); + $gejala->kode_gejala = $validatedData['kode_gejala']; + $gejala->gejala = $validatedData['gejala']; + $gejala->nilai_densitas = $validatedData['nilai_densitas']; - return redirect('gejala')->with('success', 'Data gejala berhasil disimpan.'); + // Simpan objek Gejala ke dalam database + $gejala->save(); + + // Redirect kembali ke halaman yang sesuai + return redirect('gejala')->with('success', 'Gejala berhasil ditambahkan!'); + } catch (\Exception $e) { + // Tangkap pengecualian dan tampilkan pesan kesalahan + return redirect('home')->withInput()->withErrors(['error' => $e->getMessage()]); } + } /** @@ -63,9 +76,24 @@ public function edit(string $id) /** * Update the specified resource in storage. */ - public function update(Request $request, string $id) + public function update(Request $request, string $id_gejala) { - // + $validatedData = $request->validate([ + 'kode_gejala' => 'required|string|max:255', + 'gejala' => 'required|string|max:255', // Foto produk menjadi opsional untuk diubah + 'nilai_densitas' => 'required|string|max:255', + ]); + + // Dapatkan data produk berdasarkan ID + $gejala = Gejala::findOrFail($id_gejala); + + // Perbarui data produk + $gejala->kode_gejala = $validatedData['kode_gejala']; + $gejala->gejala = $validatedData['gejala']; + $gejala->nilai_densitas = $validatedData['nilai_densitas']; + + $gejala->save(); + return redirect()->route('gejala')->with('success', 'Produk berhasil diperbarui.'); } /** diff --git a/app/Http/Controllers/PenyakitController.php b/app/Http/Controllers/PenyakitController.php index fe1d9df..edf8d6f 100644 --- a/app/Http/Controllers/PenyakitController.php +++ b/app/Http/Controllers/PenyakitController.php @@ -34,7 +34,16 @@ public function create() */ public function store(Request $request) { - // + $validatedData = $request->validate([ + 'kode_penyakit' => 'required|string', + 'nama_penyakit' => 'required|string', + 'deskripsi_penyakit' => 'required|string', + 'solusi' => 'required|string', + ]); + + Penyakit::create($validatedData); + + return redirect('penyakit')->with('success', 'Data gejala berhasil disimpan.'); } /** @@ -56,9 +65,26 @@ public function edit(string $id) /** * Update the specified resource in storage. */ - public function update(Request $request, string $id) + public function update(Request $request, string $id_penyakit) { - // + $validatedData = $request->validate([ + 'kode_penyakit' => 'required|string|max:255', + 'nama_penyakit' => 'required|string|max:255', // Foto produk menjadi opsional untuk diubah + 'deskripsi_penyakit' => 'required|string|max:255', + 'solusi' => 'required|string|max:255', + ]); + + // Dapatkan data produk berdasarkan ID + $penyakit = Penyakit::findOrFail($id_penyakit); + + // Perbarui data produk + $penyakit->kode_penyakit = $validatedData['kode_penyakit']; + $penyakit->nama_penyakit = $validatedData['nama_penyakit']; + $penyakit->deskripsi_penyakit = $validatedData['deskripsi_penyakit']; + $penyakit->solusi = $validatedData['solusi']; + + $penyakit->save(); + return redirect()->route('penyakit')->with('success', 'Penyakit berhasil diperbarui.'); } /** diff --git a/app/Models/Penyakit.php b/app/Models/Penyakit.php index 85cf31c..6a1fc95 100644 --- a/app/Models/Penyakit.php +++ b/app/Models/Penyakit.php @@ -8,6 +8,9 @@ class Penyakit extends Model { use HasFactory; + public $timestamps = false; protected $table="penyakit"; protected $primaryKey="id_penyakit"; + protected $fillable=["kode_penyakit","nama_penyakit","deskripsi_penyakit","solusi"]; + } diff --git a/resources/views/dashboard/Aturan/Aturan.blade.php b/resources/views/dashboard/Aturan/Aturan.blade.php index 8e637a9..d80c067 100644 --- a/resources/views/dashboard/Aturan/Aturan.blade.php +++ b/resources/views/dashboard/Aturan/Aturan.blade.php @@ -111,11 +111,11 @@ class="d-none d-sm-inline-block form-inline mr-auto ml-md-3 my-2 my-md-0 mw-100 - {{-- + - --}} + @@ -124,9 +124,9 @@ class="d-none d-sm-inline-block form-inline mr-auto ml-md-3 my-2 my-md-0 mw-100 @endforeach - {{-- @include('dashboard.produk.edit-produk') --}} + @include('dashboard.aturan.edit-aturan') - + {{-- + --}} diff --git a/resources/views/dashboard/Aturan/edit-aturan.blade.php b/resources/views/dashboard/Aturan/edit-aturan.blade.php new file mode 100644 index 0000000..170ce81 --- /dev/null +++ b/resources/views/dashboard/Aturan/edit-aturan.blade.php @@ -0,0 +1,70 @@ + + + + + + + + + + diff --git a/resources/views/dashboard/gejala/add-gejala.blade.php b/resources/views/dashboard/gejala/add-gejala.blade.php index 45f3975..ddbcf4c 100644 --- a/resources/views/dashboard/gejala/add-gejala.blade.php +++ b/resources/views/dashboard/gejala/add-gejala.blade.php @@ -1,4 +1,4 @@ -