From 839e9aa5726b40fe67d5492418f2baad1e17bda9 Mon Sep 17 00:00:00 2001 From: Irwan Cahyono Date: Mon, 29 Jun 2026 16:30:07 +0700 Subject: [PATCH] petani lahan --- .../Produktivitas/PetaniLahanController.php | 114 +++- app/Models/Pertanian/Lahan.php | 28 + .../2026_06_28_095941_update_lahan_table.php | 4 + .../produktivitas/petani/header.blade.php | 2 +- .../produktivitas/petani/lahan/add.blade.php | 10 +- .../produktivitas/petani/lahan/edit.blade.php | 510 ++++++++++++++++++ .../petani/lahan/index.blade.php | 5 +- 7 files changed, 649 insertions(+), 24 deletions(-) create mode 100644 resources/views/produktivitas/petani/lahan/edit.blade.php diff --git a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php index bb97bc0..9d6ef9c 100644 --- a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php +++ b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php @@ -5,12 +5,17 @@ namespace App\Http\Controllers\Web\Produktivitas; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; use App\Models\Pertanian\Petani; use App\Models\Pertanian\Lahan; use App\Models\Wilayah\DesaKelurahan; use App\Models\Wilayah\Provinsi; use App\Models\Pertanian\Komoditas; +use App\Models\Pertanian\Tanaman; class PetaniLahanController extends Controller { @@ -24,7 +29,12 @@ class PetaniLahanController extends Controller $lahan = Lahan::where('petani_id', $petaniId) ->orderBy('created_at', 'desc') - ->paginate($request->per_page ?? 10); + ->get(); + + if ($lahan->count() > 0) { + return redirect()->route('petani.lahan-tanaman.edit', [$petaniId, $lahan->first()->id]); + } + return view('produktivitas.petani.lahan.index', compact('petani', 'lahan')); } @@ -39,7 +49,7 @@ class PetaniLahanController extends Controller $lahan = Lahan::where('petani_id', $petaniId) ->orderBy('created_at', 'desc') - ->paginate($request->per_page ?? 10); + ->get(); $provinsi = Provinsi::orderBy('kode', 'asc')->get(); @@ -48,28 +58,98 @@ class PetaniLahanController extends Controller return view('produktivitas.petani.lahan.add', compact('petani', 'lahan', 'provinsi', 'komoditas')); } - /** - * Store a newly created resource in storage. - */ - public function store(Request $request) + public function store($petaniId, Request $request) { - // + if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) { + abort(403); + } + + $petani = Petani::findOrFail($petaniId); + + $validated = $request->validate([ + 'nama' => ['nullable', 'string', 'max:255'], + 'status_kepemilikan' => ['nullable', 'string', 'max:255'], + 'profile_id' => ['nullable'], + 'petani_id' => ['nullable'], + 'keluarga_pengelola_id' => ['nullable'], + 'adat_pemilik_id' => ['nullable'], + 'desa_kelurahan_id' => ['nullable'], + 'luas_lahan' => ['required', 'numeric'], + 'path' => ['nullable', 'string'], + 'komoditas_id' => ['required', 'exists:master_komoditas,id'], + 'jenis_komoditas_id' => ['required', 'exists:jenis_komoditas,id'], + 'varietas_id' => ['nullable', 'exists:master_varietas,id'], + 'jumlah_tanaman' => ['required', 'numeric'], + ]); + + $komoditas = Komoditas::where('id', $request->komoditas_id)->first(); + $lahanCount = Lahan::where('petani_id', $petaniId) + ->count(); + + $lahan = Lahan::create([...$validated, + 'nama' => 'Lahan ' . $komoditas->nama . ' #' . ($lahanCount + 1), + 'uid' => 'LHN-' . $petani->uid . '-' . strtoupper(Str::random(10)), + 'petani_id' => $petaniId, + 'created_by' => auth()->id(), + 'updated_by' => auth()->id()] + ); + + if ($lahan) { + $data = collect(range(1, $lahan->jumlah_tanaman))->map(function ($i) use ($lahan, $komoditas) { + return [ + 'lahan_id' => $lahan->id, + 'komoditas_id' => $lahan->komoditas_id, + 'jenis_komoditas_id' => $lahan->jenis_komoditas_id, + 'varietas_id' => $lahan->varietas_id, + 'petani_id' => $lahan->petani_id, + 'nama' => 'Tanaman ' . $komoditas->nama . ' #' . $i, + ]; + }); + + $data->chunk(500)->each(function ($chunk) { + Tanaman::insert($chunk->toArray()); + }); + + return redirect()->route('petani.lahan-tanaman.index', $petaniId)->with('success', 'Lahan berhasil ditambahkan.'); + } } - /** - * Display the specified resource. - */ - public function show(string $id) + public function show(string $petaniId, string $id) { - // + if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) { + abort(403); + } + + $petani = Petani::findOrFail($petaniId); + $lahan = Lahan::where('petani_id', $petaniId) + ->orderBy('created_at', 'desc') + ->get(); + + $lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id); + + $provinsi = Provinsi::orderBy('kode', 'asc')->get(); + + return view('produktivitas.petani.lahan.view', compact('lahan', 'lahanSelected', 'petani', 'provinsi')); } - /** - * Show the form for editing the specified resource. - */ - public function edit(string $id) + public function edit(string $petaniId, string $id) { - // + if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) { + abort(403); + } + + $petani = Petani::findOrFail($petaniId); + $lahan = Lahan::where('petani_id', $petaniId) + ->orderBy('created_at', 'desc') + ->get(); + + $lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id); + + $komoditas = Komoditas::all(); + + $provinsi = Provinsi::orderBy('kode', 'asc')->get(); + + return view('produktivitas.petani.lahan.edit', compact('lahan', 'lahanSelected', 'petani', 'komoditas', 'provinsi')); } /** diff --git a/app/Models/Pertanian/Lahan.php b/app/Models/Pertanian/Lahan.php index 0658438..512257b 100644 --- a/app/Models/Pertanian/Lahan.php +++ b/app/Models/Pertanian/Lahan.php @@ -11,6 +11,9 @@ use App\Models\Pertanian\Adat; use App\Enums\StatusLahan; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; +use App\Models\Pertanian\Komoditas; +use App\Models\Master\JenisKomoditas; +use App\Models\Master\Varietas; class Lahan extends Model { @@ -28,6 +31,11 @@ class Lahan extends Model 'desa_kelurahan_id', 'path', 'luas_lahan', + 'komoditas_id', + 'jenis_komoditas_id', + 'varietas_id', + 'jumlah_tanaman', + 'petani_id', 'created_by', 'updated_by', 'deleted_by', @@ -57,4 +65,24 @@ class Lahan extends Model { return $this->hasMany(Tanaman::class, 'lahan_id'); } + + public function petani(): BelongsTo + { + return $this->belongsTo(Profile::class, 'petani_id'); + } + + public function komoditas(): BelongsTo + { + return $this->belongsTo(Komoditas::class, 'komoditas_id'); + } + + public function jenisKomoditas(): BelongsTo + { + return $this->belongsTo(JenisKomoditas::class, 'jenis_komoditas_id'); + } + + public function varietas(): BelongsTo + { + return $this->belongsTo(Varietas::class, 'varietas_id'); + } } diff --git a/database/migrations/2026_06_28_095941_update_lahan_table.php b/database/migrations/2026_06_28_095941_update_lahan_table.php index a54c02e..cb72450 100644 --- a/database/migrations/2026_06_28_095941_update_lahan_table.php +++ b/database/migrations/2026_06_28_095941_update_lahan_table.php @@ -35,6 +35,10 @@ return new class extends Migration Schema::table('petani', function (Blueprint $table) { $table->foreignId('profile_id')->nullable()->constrained('profiles')->onDelete('set null'); }); + + Schema::table('map_petani', function (Blueprint $table) { + $table->foreignId('petani_id')->nullable()->constrained('petani')->onDelete('set null'); + }); } /** diff --git a/resources/views/produktivitas/petani/header.blade.php b/resources/views/produktivitas/petani/header.blade.php index 17434c6..dfe2c65 100644 --- a/resources/views/produktivitas/petani/header.blade.php +++ b/resources/views/produktivitas/petani/header.blade.php @@ -67,7 +67,7 @@ diff --git a/resources/views/produktivitas/petani/lahan/add.blade.php b/resources/views/produktivitas/petani/lahan/add.blade.php index e281675..4a7cc7a 100644 --- a/resources/views/produktivitas/petani/lahan/add.blade.php +++ b/resources/views/produktivitas/petani/lahan/add.blade.php @@ -42,7 +42,7 @@