From 9df3b3403d3d81aedfe21286b3812bac7322b31b Mon Sep 17 00:00:00 2001 From: rahmagustin Date: Tue, 13 Jan 2026 10:55:04 +0700 Subject: [PATCH] crud sampah --- .../Controllers/Admin/SampahController.php | 100 ++++++++++++ app/Models/Sampah.php | 20 ++- .../views/admin/kategori-tps/index.blade.php | 11 +- resources/views/admin/sampah/create.blade.php | 143 ++++++++++++------ resources/views/admin/sampah/edit.blade.php | 127 ++++++++++------ resources/views/admin/sampah/index.blade.php | 135 ++++++++++------- resources/views/admin/template.blade.php | 116 +------------- resources/views/admin/tps/create.blade.php | 2 +- resources/views/admin/tps/edit.blade.php | 2 +- routes/web.php | 9 ++ 10 files changed, 390 insertions(+), 275 deletions(-) create mode 100644 app/Http/Controllers/Admin/SampahController.php diff --git a/app/Http/Controllers/Admin/SampahController.php b/app/Http/Controllers/Admin/SampahController.php new file mode 100644 index 0000000..b738692 --- /dev/null +++ b/app/Http/Controllers/Admin/SampahController.php @@ -0,0 +1,100 @@ +orderBy('tahun', 'desc')->get(); + + return view('admin.sampah.index', compact('title', 'sampah')); + } + + public function create() + { + $title = 'Tambah Data Sampah'; + $users = User::all(); + + return view('admin.sampah.create', compact('title', 'users')); + } + + public function store(Request $request) + { + $request->validate([ + 'tahun' => 'required|numeric', + 'total_sampah' => 'required|numeric', + 'total_kelola' => 'required|numeric', + 'total_daur_ulang' => 'required|numeric', + ]); + + $sisa_sampah = $request->total_sampah + - ($request->total_kelola + $request->total_daur_ulang); + + Sampah::create([ + 'user_id' => Auth::id(), + 'tahun' => $request->tahun, + 'total_sampah' => $request->total_sampah, + 'total_kelola' => $request->total_kelola, + 'total_daur_ulang' => $request->total_daur_ulang, + 'sisa_sampah' => $sisa_sampah, + ]); + + return redirect()->route('admin.sampah.index') + ->with('success', 'Data sampah berhasil ditambahkan'); + } + + public function edit($id) + { + $title = 'Edit Data Sampah'; + $sampah = Sampah::findOrFail($id); + $users = User::all(); + + return view('admin.sampah.edit', compact('title', 'sampah', 'users')); + } + + public function update(Request $request, $id) + { + $sampah = Sampah::findOrFail($id); + + $request->validate([ + 'user_id' => 'required|exists:users,id', + 'tahun' => 'required|numeric', + 'total_sampah' => 'required|numeric', + 'total_kelola' => 'required|numeric', + 'total_daur_ulang' => 'required|numeric', + ]); + + $sisa_sampah = $request->total_sampah + - ($request->total_kelola + $request->total_daur_ulang); + + $sampah->update([ + 'user_id' => $request->user_id, + 'tahun' => $request->tahun, + 'total_sampah' => $request->total_sampah, + 'total_kelola' => $request->total_kelola, + 'total_daur_ulang' => $request->total_daur_ulang, + 'sisa_sampah' => $sisa_sampah, + ]); + + return redirect()->route('admin.sampah.index') + ->with('success', 'Data sampah berhasil diperbarui'); + } + + public function destroy($id) + { + $sampah = Sampah::findOrFail($id); + $sampah->delete(); + + return redirect()->route('admin.sampah.index') + ->with('success', 'Data sampah berhasil dihapus'); + } +} diff --git a/app/Models/Sampah.php b/app/Models/Sampah.php index 66bbe86..e52ad11 100644 --- a/app/Models/Sampah.php +++ b/app/Models/Sampah.php @@ -6,5 +6,23 @@ class Sampah extends Model { - // + protected $table = 'sampah'; + + protected $fillable = [ + 'user_id', + 'tahun', + 'total_sampah', + 'total_kelola', + 'total_daur_ulang', + 'sisa_sampah', + ]; + + /** + * Relasi ke tabel users + * One Sampah belongs to one User + */ + public function user() + { + return $this->belongsTo(User::class, 'user_id'); + } } diff --git a/resources/views/admin/kategori-tps/index.blade.php b/resources/views/admin/kategori-tps/index.blade.php index 5c2bae4..2c1700e 100644 --- a/resources/views/admin/kategori-tps/index.blade.php +++ b/resources/views/admin/kategori-tps/index.blade.php @@ -7,9 +7,14 @@
-

Kategori TPS

+
+

Data Kategori TPS

+

+ Daftar Kategori Tempat Pengelolaan Sampah (TPS) +

+
- + Tambah + Tambah
@@ -72,7 +77,7 @@ class="form-hapus" style="display:inline;"> e.preventDefault(); Swal.fire({ - title: 'Hapus Data TPS?', + title: 'Hapus Data Kategori TPS?', text: 'Data yang sudah dihapus tidak dapat dikembalikan!', icon: 'warning', showCancelButton: true, diff --git a/resources/views/admin/sampah/create.blade.php b/resources/views/admin/sampah/create.blade.php index 8b801bb..7d2e4aa 100644 --- a/resources/views/admin/sampah/create.blade.php +++ b/resources/views/admin/sampah/create.blade.php @@ -1,62 +1,105 @@ @extends('admin.template') @section('content') -
-
-
-
-
-

Basic form elements

-

- Basic form elements -

-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- - - - +
+
+
+
+
+

Tambah Data Sampah Tahunan

+

+ Form input data sampah per tahun (satuan dalam Ton) +

+ + + @csrf + +
+ + +
+ +
+ +
+ +
+ Ton
-
- - +
+ +
+ +
+ +
+ Ton +
-
- - +
+ +
+ +
+ +
+ Ton +
- - - -
+
+ +
+ +
+ +
+ Ton +
+
+ + Sisa sampah dihitung otomatis + +
+ + + + Batal + + +
+
+ + @endsection diff --git a/resources/views/admin/sampah/edit.blade.php b/resources/views/admin/sampah/edit.blade.php index 8b801bb..3cb277a 100644 --- a/resources/views/admin/sampah/edit.blade.php +++ b/resources/views/admin/sampah/edit.blade.php @@ -1,62 +1,89 @@ @extends('admin.template') @section('content') -
-
-
-
-
-

Basic form elements

-

- Basic form elements -

-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- - - - +
+
+
+
+
+

Edit Data Sampah Tahunan

+

+ Form edit data sampah per tahun (satuan Ton) +

+ + + @csrf + @method('PUT') + +
+ + +
+ +
+ +
+ +
+ Ton
-
- - +
+ +
+ +
+ +
+ Ton +
-
- - +
+ +
+ +
+ +
+ Ton +
- - - -
+
+ +
+ +
+ +
+ Ton +
+
+ + Sisa sampah dihitung otomatis oleh sistem + +
+ + + + Batal + + +
+
@endsection diff --git a/resources/views/admin/sampah/index.blade.php b/resources/views/admin/sampah/index.blade.php index 164d64d..8cc419c 100644 --- a/resources/views/admin/sampah/index.blade.php +++ b/resources/views/admin/sampah/index.blade.php @@ -7,16 +7,15 @@
-
-

Basic Table

+

Data Sampah

- Add class .table + Daftar Sampah Per Tahun di Kabupaten Nganjuk

- + Tambah @@ -28,62 +27,47 @@ - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @forelse ($sampah as $item) + + + + + + + + + @empty + + + + @endforelse
ProfileVatNo.CreatedStatusTahunTotal SampahTotal KelolaTotal Daur UlangSisa Sampah Aksi
Jacob5327553112 May 2017 - - - - - - -
Messsy5327553215 May 2017 - - - - - - -
John5327553314 May 2017
Peter5327553416 May 2017
Dave5327553520 May 2017
{{ $item->tahun }}{{ $item->total_sampah }}{{ $item->total_kelola }}{{ $item->total_daur_ulang }}{{ $item->sisa_sampah }} + + + + +
+ @csrf + @method('DELETE') + +
+ +
+ Data sampah belum tersedia +
@@ -93,4 +77,43 @@
+ + + + @if (session('success')) + + @endif @endsection diff --git a/resources/views/admin/template.blade.php b/resources/views/admin/template.blade.php index 1cac8c1..07e6dea 100644 --- a/resources/views/admin/template.blade.php +++ b/resources/views/admin/template.blade.php @@ -88,119 +88,9 @@ - - - - - - - diff --git a/resources/views/admin/tps/create.blade.php b/resources/views/admin/tps/create.blade.php index 398d59c..8235aea 100644 --- a/resources/views/admin/tps/create.blade.php +++ b/resources/views/admin/tps/create.blade.php @@ -6,7 +6,7 @@
-

Tambah TPS

+

Tambah Data TPS

Form tambah data TPS

diff --git a/resources/views/admin/tps/edit.blade.php b/resources/views/admin/tps/edit.blade.php index 3ec7801..7d25e88 100644 --- a/resources/views/admin/tps/edit.blade.php +++ b/resources/views/admin/tps/edit.blade.php @@ -6,7 +6,7 @@
-

Edit TPS

+

Edit Data TPS

Form edit data Tempat Pembuangan Sampah

diff --git a/routes/web.php b/routes/web.php index d238269..e69dc8c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Admin\DashboardController; use App\Http\Controllers\Admin\KategoriTpsController; use App\Http\Controllers\Admin\LoginController; +use App\Http\Controllers\Admin\SampahController; use App\Http\Controllers\Admin\TpsController; use App\Http\Controllers\IndexController; use App\Http\Controllers\AduanController; @@ -66,4 +67,12 @@ Route::get('/kategori-tps/{id}/edit', [KategoriTpsController::class, 'edit'])->name('kategori.edit'); Route::put('/kategori-tps/{id}', [KategoriTpsController::class, 'update'])->name('kategori.update'); Route::delete('/kategori-tps/{id}', [KategoriTpsController::class, 'destroy'])->name('kategori.destroy'); + + // SAMPAH ADMIN + Route::get('/sampah', [SampahController::class, 'index'])->name('sampah.index'); + Route::get('/sampah/create', [SampahController::class, 'create'])->name('sampah.create'); + Route::post('/sampah', [SampahController::class, 'store'])->name('sampah.store'); + Route::get('/sampah/{id}/edit', [SampahController::class, 'edit'])->name('sampah.edit'); + Route::put('/sampah/{id}', [SampahController::class, 'update'])->name('sampah.update'); + Route::delete('/sampah/{id}', [SampahController::class, 'destroy'])->name('sampah.destroy'); });