petani lahan tanaman update
This commit is contained in:
@@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rules\Enum;
|
||||
|
||||
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
@@ -17,6 +19,12 @@ use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\Pertanian\Tanaman;
|
||||
|
||||
use App\Enums\StatusLahan;
|
||||
use App\Enums\KondisiTanaman;
|
||||
use App\Enums\SeranganHama;
|
||||
use App\Enums\PenyakitTanaman;
|
||||
use App\Enums\KeparahanSerangan;
|
||||
|
||||
class PetaniLahanController extends Controller
|
||||
{
|
||||
public function index($petaniId, Request $request)
|
||||
@@ -80,6 +88,12 @@ class PetaniLahanController extends Controller
|
||||
'jenis_komoditas_id' => ['required', 'exists:jenis_komoditas,id'],
|
||||
'varietas_id' => ['nullable', 'exists:master_varietas,id'],
|
||||
'jumlah_tanaman' => ['required', 'numeric'],
|
||||
'umur_lahan' => ['required', 'numeric'],
|
||||
'umur_tanaman' => ['required', 'numeric'],
|
||||
'kondisi_tanaman' => ['required', 'string', new Enum(KondisiTanaman::class)],
|
||||
'serangan_hama' => ['nullable', 'string', new Enum(SeranganHama::class)],
|
||||
'penyakit_tanaman' => ['nullable', 'string', new Enum(PenyakitTanaman::class)],
|
||||
'keparahan_serangan' => ['nullable', 'string', new Enum(KeparahanSerangan::class)],
|
||||
]);
|
||||
|
||||
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
||||
@@ -103,6 +117,11 @@ class PetaniLahanController extends Controller
|
||||
'varietas_id' => $lahan->varietas_id,
|
||||
'petani_id' => $lahan->petani_id,
|
||||
'nama' => 'Tanaman ' . $komoditas->nama . ' #' . $i,
|
||||
'umur_tanaman' => $lahan->umur_tanaman,
|
||||
'kondisi_tanaman' => $lahan->kondisi_tanaman,
|
||||
'serangan_hama' => $lahan->serangan_hama,
|
||||
'penyakit_tanaman' => $lahan->penyakit_tanaman,
|
||||
'keparahan_serangan' => $lahan->keparahan_serangan
|
||||
];
|
||||
});
|
||||
|
||||
@@ -122,7 +141,7 @@ class PetaniLahanController extends Controller
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)
|
||||
->orderBy('created_at', 'desc')
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
$lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||
@@ -140,7 +159,7 @@ class PetaniLahanController extends Controller
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)
|
||||
->orderBy('created_at', 'desc')
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
$lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||
@@ -155,9 +174,67 @@ class PetaniLahanController extends Controller
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
public function update(Request $request, string $petaniId, string $id)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||
|
||||
$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'],
|
||||
'umur_lahan' => ['required', 'numeric'],
|
||||
'umur_tanaman' => ['required', 'numeric'],
|
||||
'kondisi_tanaman' => ['required', 'string', new Enum(KondisiTanaman::class)],
|
||||
'serangan_hama' => ['nullable', 'string', new Enum(SeranganHama::class)],
|
||||
'penyakit_tanaman' => ['nullable', 'string', new Enum(PenyakitTanaman::class)],
|
||||
'keparahan_serangan' => ['nullable', 'string', new Enum(KeparahanSerangan::class)],
|
||||
]);
|
||||
|
||||
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
||||
|
||||
$lahanCount = Lahan::where('petani_id', $petaniId)
|
||||
->count();
|
||||
|
||||
$lahan->update([...$validated,
|
||||
'nama' => 'Lahan ' . $komoditas->nama . ' #' . ($lahanCount + 1),
|
||||
'uid' => 'LHN-' . $petani->uid . '-' . strtoupper(Str::random(10)),
|
||||
'petani_id' => $petaniId,
|
||||
'updated_by' => auth()->id()]
|
||||
);
|
||||
|
||||
if ($lahan) {
|
||||
$data = collect(range($lahanCount, $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.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Produktivitas;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\Rules\Enum;
|
||||
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\Pertanian\Tanaman;
|
||||
|
||||
use App\Enums\KondisiTanaman;
|
||||
|
||||
class PetaniLahanTanamanController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $petaniId, string $lahanId, string $id)
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)->findOrFail($lahanId);
|
||||
$lahanTanaman = Tanaman::with(['komoditas', 'jenisKomoditas', 'varietas', 'profile'])->where('lahan_id', $lahanId)->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $lahanTanaman,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function update(Request $request, string $petaniId, string $lahanId, string $id)
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)->findOrFail($lahanId);
|
||||
$lahanTanaman = Tanaman::where('lahan_id', $lahanId)->findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'latitude' => ['nullable', 'numeric', 'between:-90,90', 'required_with:longitude'],
|
||||
'longitude' => ['nullable', 'numeric', 'between:-180,180', 'required_with:latitude'],
|
||||
'kondisi_tanaman' => ['nullable', 'string', new Enum(KondisiTanaman::class)],
|
||||
]);
|
||||
|
||||
$lahanTanaman->update([...$validated, 'updated_by' => auth()->id()]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data tanaman berhasil diperbarui',
|
||||
'data' => $lahanTanaman,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $petaniId, string $lahanId, string $id)
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)->findOrFail($lahanId);
|
||||
$lahanTanaman = LahanTanaman::where('lahan_id', $lahanId)->findOrFail($id);
|
||||
|
||||
$lahanTanaman->updated_by = auth()->id();
|
||||
$lahanTanaman->save();
|
||||
$lahanTanaman->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data tanaman berhasil dihapus',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user