tanaman penaung di lahan
This commit is contained in:
@@ -18,6 +18,7 @@ use App\Models\Wilayah\DesaKelurahan;
|
|||||||
use App\Models\Wilayah\Provinsi;
|
use App\Models\Wilayah\Provinsi;
|
||||||
use App\Models\Pertanian\Komoditas;
|
use App\Models\Pertanian\Komoditas;
|
||||||
use App\Models\Pertanian\Tanaman;
|
use App\Models\Pertanian\Tanaman;
|
||||||
|
use App\Models\Master\JenisTanaman;
|
||||||
|
|
||||||
use App\Enums\StatusLahan;
|
use App\Enums\StatusLahan;
|
||||||
use App\Enums\KondisiTanaman;
|
use App\Enums\KondisiTanaman;
|
||||||
@@ -62,8 +63,9 @@ class PetaniLahanController extends Controller
|
|||||||
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
||||||
|
|
||||||
$komoditas = Komoditas::all();
|
$komoditas = Komoditas::all();
|
||||||
|
$tanamanPenaung = JenisTanaman::all();
|
||||||
|
|
||||||
return view('produktivitas.petani.lahan.add', compact('petani', 'lahan', 'provinsi', 'komoditas'));
|
return view('produktivitas.petani.lahan.add', compact('petani', 'lahan', 'provinsi', 'komoditas', 'tanamanPenaung'));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateLahan(Request $request)
|
private function validateLahan(Request $request)
|
||||||
@@ -91,7 +93,7 @@ class PetaniLahanController extends Controller
|
|||||||
'komoditas_lain' => ['nullable', 'array'],
|
'komoditas_lain' => ['nullable', 'array'],
|
||||||
'komoditas_lain.*' => ['integer', 'exists:master_komoditas,id'],
|
'komoditas_lain.*' => ['integer', 'exists:master_komoditas,id'],
|
||||||
'tanaman_penaung' => ['nullable', 'array'],
|
'tanaman_penaung' => ['nullable', 'array'],
|
||||||
'tanaman_penaung.*' => ['integer', 'exists:master_komoditas,id'],
|
'tanaman_penaung.*' => ['string'],
|
||||||
'foto' => ['nullable', 'array'],
|
'foto' => ['nullable', 'array'],
|
||||||
'foto.*' => ['image', 'mimes:jpeg,jpg,png', 'max:2048'],
|
'foto.*' => ['image', 'mimes:jpeg,jpg,png', 'max:2048'],
|
||||||
], [
|
], [
|
||||||
@@ -119,7 +121,7 @@ class PetaniLahanController extends Controller
|
|||||||
'komoditas_lain.array' => 'Komoditas lain diisi dengan array.',
|
'komoditas_lain.array' => 'Komoditas lain diisi dengan array.',
|
||||||
'komoditas_lain.*.exists' => 'Komoditas lain yang dipilih tidak valid.',
|
'komoditas_lain.*.exists' => 'Komoditas lain yang dipilih tidak valid.',
|
||||||
'tanaman_penaung.array' => 'Tanaman penaung diisi dengan array.',
|
'tanaman_penaung.array' => 'Tanaman penaung diisi dengan array.',
|
||||||
'tanaman_penaung.*.exists' => 'Tanaman penaung yang dipilih tidak valid.',
|
//'tanaman_penaung.*.exists' => 'Tanaman penaung yang dipilih tidak valid.',
|
||||||
'foto.array' => 'Foto diisi dengan array.',
|
'foto.array' => 'Foto diisi dengan array.',
|
||||||
'foto.*.image' => 'Foto harus berupa gambar.',
|
'foto.*.image' => 'Foto harus berupa gambar.',
|
||||||
'foto.*.mimes' => 'Foto harus berupa gambar dengan ekstensi jpeg, jpg, atau png.',
|
'foto.*.mimes' => 'Foto harus berupa gambar dengan ekstensi jpeg, jpg, atau png.',
|
||||||
@@ -128,6 +130,28 @@ class PetaniLahanController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function syncTanamanPenaung(Request $request)
|
||||||
|
{
|
||||||
|
$existing = JenisTanaman::pluck('nama')->toArray();
|
||||||
|
|
||||||
|
$tanamanPenaung = collect($request->tanaman_penaung)
|
||||||
|
->unique()
|
||||||
|
->reject(fn ($nama) => in_array($nama, $existing))
|
||||||
|
->map(fn ($nama) => [
|
||||||
|
'nama' => Str::title($nama),
|
||||||
|
'created_by' => auth()->id(),
|
||||||
|
'updated_by' => auth()->id(),
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
])
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
if (!empty($tanamanPenaung)) {
|
||||||
|
JenisTanaman::insert($tanamanPenaung);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function store($petaniId, Request $request)
|
public function store($petaniId, Request $request)
|
||||||
{
|
{
|
||||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||||
@@ -136,6 +160,12 @@ class PetaniLahanController extends Controller
|
|||||||
|
|
||||||
$petani = Petani::findOrFail($petaniId);
|
$petani = Petani::findOrFail($petaniId);
|
||||||
|
|
||||||
|
$request->merge([
|
||||||
|
'tanaman_penaung' => collect($request->tanaman_penaung)
|
||||||
|
->map(fn ($nama) => Str::title($nama))
|
||||||
|
->toArray()
|
||||||
|
]);
|
||||||
|
|
||||||
$validated = $this->validateLahan($request);
|
$validated = $this->validateLahan($request);
|
||||||
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
||||||
$lahanCount = Lahan::where('petani_id', $petaniId)
|
$lahanCount = Lahan::where('petani_id', $petaniId)
|
||||||
@@ -154,6 +184,8 @@ class PetaniLahanController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->syncTanamanPenaung($request);
|
||||||
|
|
||||||
$lahan = Lahan::create([...$validated,
|
$lahan = Lahan::create([...$validated,
|
||||||
'foto' => $foto && count($foto) > 0 ? json_encode($foto) : null,
|
'foto' => $foto && count($foto) > 0 ? json_encode($foto) : null,
|
||||||
'nama' => 'Lahan ' . $komoditas->nama . ' #' . ($lahanCount + 1),
|
'nama' => 'Lahan ' . $komoditas->nama . ' #' . ($lahanCount + 1),
|
||||||
@@ -236,10 +268,11 @@ class PetaniLahanController extends Controller
|
|||||||
$lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
$lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||||
|
|
||||||
$komoditas = Komoditas::all();
|
$komoditas = Komoditas::all();
|
||||||
|
$tanamanPenaung = JenisTanaman::all();
|
||||||
|
|
||||||
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
||||||
|
|
||||||
return view('produktivitas.petani.lahan.edit', compact('lahan', 'lahanSelected', 'petani', 'komoditas', 'provinsi'));
|
return view('produktivitas.petani.lahan.edit', compact('lahan', 'lahanSelected', 'petani', 'komoditas', 'provinsi', 'tanamanPenaung'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Request $request, string $petaniId, string $id)
|
public function update(Request $request, string $petaniId, string $id)
|
||||||
@@ -251,6 +284,11 @@ class PetaniLahanController extends Controller
|
|||||||
$petani = Petani::findOrFail($petaniId);
|
$petani = Petani::findOrFail($petaniId);
|
||||||
$lahan = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
$lahan = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||||
|
|
||||||
|
$request->merge([
|
||||||
|
'tanaman_penaung' => collect($request->tanaman_penaung)
|
||||||
|
->map(fn ($nama) => Str::title($nama))
|
||||||
|
->toArray()
|
||||||
|
]);
|
||||||
$validated = $this->validateLahan($request);
|
$validated = $this->validateLahan($request);
|
||||||
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
||||||
$tanamanCount = Tanaman::where('lahan_id', $lahan->id)->count();
|
$tanamanCount = Tanaman::where('lahan_id', $lahan->id)->count();
|
||||||
@@ -266,6 +304,8 @@ class PetaniLahanController extends Controller
|
|||||||
$lahanUidParts = explode('LHN-', $lahanUid);
|
$lahanUidParts = explode('LHN-', $lahanUid);
|
||||||
$lahanUid = end($lahanUidParts);
|
$lahanUid = end($lahanUidParts);
|
||||||
|
|
||||||
|
$this->syncTanamanPenaung($request);
|
||||||
|
|
||||||
if ($lahan && $lahan->jumlah_tanaman > $tanamanCount) {
|
if ($lahan && $lahan->jumlah_tanaman > $tanamanCount) {
|
||||||
$data = collect(range($tanamanCount + 1, $lahan->jumlah_tanaman))->map(function ($i) use ($lahan, $komoditas, $lahanUid) {
|
$data = collect(range($tanamanCount + 1, $lahan->jumlah_tanaman))->map(function ($i) use ($lahan, $komoditas, $lahanUid) {
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Master;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
||||||
|
class JenisTanaman extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
protected $table = 'master_jenis_tanaman';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'nama',
|
||||||
|
'created_by',
|
||||||
|
'updated_by',
|
||||||
|
'deleted_by',
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('master_jenis_tanaman', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('nama');
|
||||||
|
|
||||||
|
$table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
|
||||||
|
$table->foreignId('updated_by')->nullable()->constrained('users')->onDelete('set null');
|
||||||
|
$table->foreignId('deleted_by')->nullable()->constrained('users')->onDelete('set null');
|
||||||
|
|
||||||
|
$table->timestamps();
|
||||||
|
$table->softDeletes();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('jenis_tanamen');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -302,9 +302,9 @@
|
|||||||
<div class="col-md-6 mb-3 form-control-validation">
|
<div class="col-md-6 mb-3 form-control-validation">
|
||||||
<label class="form-label">Tanaman Penaung</label>
|
<label class="form-label">Tanaman Penaung</label>
|
||||||
<select id="tanaman_penaung" name="tanaman_penaung[]" class="form-select @error('tanaman_penaung[]') is-invalid @enderror" multiple>
|
<select id="tanaman_penaung" name="tanaman_penaung[]" class="form-select @error('tanaman_penaung[]') is-invalid @enderror" multiple>
|
||||||
<option value="">Pilih Komoditas</option>
|
<option value="">Pilih Tanaman atau ketik jika tidak ada</option>
|
||||||
@foreach($komoditas as $k)
|
@foreach($tanamanPenaung as $k)
|
||||||
<option value="{{ $k->id }}">{{ $k->nama }}</option>
|
<option value="{{ $k->nama }}">{{ $k->nama }}</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
@error('tanaman_penaung[]')<div class="error invalid-feedback">{{ $message }}</div>@enderror
|
@error('tanaman_penaung[]')<div class="error invalid-feedback">{{ $message }}</div>@enderror
|
||||||
@@ -429,7 +429,9 @@
|
|||||||
$('#penyakit_tanaman').select2();
|
$('#penyakit_tanaman').select2();
|
||||||
$('#keparahan_serangan').select2();
|
$('#keparahan_serangan').select2();
|
||||||
$('#komoditas_lain').select2();
|
$('#komoditas_lain').select2();
|
||||||
$('#tanaman_penaung').select2();
|
$('#tanaman_penaung').select2({
|
||||||
|
tags: true
|
||||||
|
});
|
||||||
|
|
||||||
$('#provinsi_id').select2().change(function(e) {
|
$('#provinsi_id').select2().change(function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|||||||
@@ -164,10 +164,10 @@
|
|||||||
<div class="col-md-6 mb-3 form-control-validation">
|
<div class="col-md-6 mb-3 form-control-validation">
|
||||||
<label class="form-label">Tanaman Penaung</label>
|
<label class="form-label">Tanaman Penaung</label>
|
||||||
<select id="tanaman_penaung" name="tanaman_penaung[]" class="form-select @error('tanaman_penaung[]') is-invalid @enderror" multiple>
|
<select id="tanaman_penaung" name="tanaman_penaung[]" class="form-select @error('tanaman_penaung[]') is-invalid @enderror" multiple>
|
||||||
<option value="">Pilih Komoditas</option>
|
<option value="">Pilih Tanaman atau ketik jika tidak ada</option>
|
||||||
@foreach($komoditas as $k)
|
@foreach($tanamanPenaung as $k)
|
||||||
<option value="{{ $k->id }}"
|
<option value="{{ $k->nama }}"
|
||||||
{{ in_array($k->id, old('tanaman_penaung', $lahanSelected->tanaman_penaung ?? [])) ? 'selected' : '' }}>
|
{{ in_array($k->nama, old('tanaman_penaung', $lahanSelected->tanaman_penaung ?? [])) ? 'selected' : '' }}>
|
||||||
{{ $k->nama }}
|
{{ $k->nama }}
|
||||||
</option>
|
</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
@@ -379,7 +379,9 @@
|
|||||||
$('#penyakit_tanaman').select2();
|
$('#penyakit_tanaman').select2();
|
||||||
$('#keparahan_serangan').select2();
|
$('#keparahan_serangan').select2();
|
||||||
$('#komoditas_lain').select2();
|
$('#komoditas_lain').select2();
|
||||||
$('#tanaman_penaung').select2();
|
$('#tanaman_penaung').select2({
|
||||||
|
tags: true
|
||||||
|
});
|
||||||
|
|
||||||
$('#table-tanaman').DataTable({
|
$('#table-tanaman').DataTable({
|
||||||
"paging": true,
|
"paging": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user