update hama dan penyakit
This commit is contained in:
@@ -7,6 +7,7 @@ use Illuminate\Http\Request;
|
|||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Validation\Rules\Enum;
|
use Illuminate\Validation\Rules\Enum;
|
||||||
@@ -101,6 +102,12 @@ class PetaniLahanController extends Controller
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function hasPestDiseaseColumns(string $table): bool
|
||||||
|
{
|
||||||
|
return Schema::hasColumn($table, 'jenis_hama')
|
||||||
|
&& Schema::hasColumn($table, 'jenis_penyakit');
|
||||||
|
}
|
||||||
|
|
||||||
private function validateLahan(Request $request)
|
private function validateLahan(Request $request)
|
||||||
{
|
{
|
||||||
return $request->validate([
|
return $request->validate([
|
||||||
@@ -200,7 +207,14 @@ class PetaniLahanController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$validated = $this->validateLahan($request);
|
$validated = $this->validateLahan($request);
|
||||||
$validated = array_merge($validated, $this->autoPestDiseaseData($petaniId));
|
$autoPestDiseaseData = $this->autoPestDiseaseData($petaniId);
|
||||||
|
$lahanHasPestDiseaseColumns = $this->hasPestDiseaseColumns((new Lahan)->getTable());
|
||||||
|
$tanamanHasPestDiseaseColumns = $this->hasPestDiseaseColumns((new Tanaman)->getTable());
|
||||||
|
|
||||||
|
if ($lahanHasPestDiseaseColumns) {
|
||||||
|
$validated = array_merge($validated, $autoPestDiseaseData);
|
||||||
|
}
|
||||||
|
|
||||||
$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)
|
||||||
->count();
|
->count();
|
||||||
@@ -230,8 +244,8 @@ class PetaniLahanController extends Controller
|
|||||||
);
|
);
|
||||||
|
|
||||||
if ($lahan && $lahan->jumlah_tanaman > 0) {
|
if ($lahan && $lahan->jumlah_tanaman > 0) {
|
||||||
$data = collect(range(1, $lahan->jumlah_tanaman))->map(function ($i) use ($lahan, $komoditas, $lahanUid) {
|
$data = collect(range(1, $lahan->jumlah_tanaman))->map(function ($i) use ($lahan, $komoditas, $lahanUid, $autoPestDiseaseData, $tanamanHasPestDiseaseColumns) {
|
||||||
return [
|
$row = [
|
||||||
'lahan_id' => $lahan->id,
|
'lahan_id' => $lahan->id,
|
||||||
'uid' => 'TNM-' . $lahanUid . '-' . strtoupper(Str::random(10)),
|
'uid' => 'TNM-' . $lahanUid . '-' . strtoupper(Str::random(10)),
|
||||||
'komoditas_id' => $lahan->komoditas_id,
|
'komoditas_id' => $lahan->komoditas_id,
|
||||||
@@ -244,13 +258,18 @@ class PetaniLahanController extends Controller
|
|||||||
'serangan_hama' => $lahan->serangan_hama,
|
'serangan_hama' => $lahan->serangan_hama,
|
||||||
'penyakit_tanaman' => $lahan->penyakit_tanaman,
|
'penyakit_tanaman' => $lahan->penyakit_tanaman,
|
||||||
'keparahan_serangan' => $lahan->keparahan_serangan,
|
'keparahan_serangan' => $lahan->keparahan_serangan,
|
||||||
'jenis_hama' => json_encode($lahan->jenis_hama ?? []),
|
|
||||||
'jenis_penyakit' => json_encode($lahan->jenis_penyakit ?? []),
|
|
||||||
'created_by' => auth()->id(),
|
'created_by' => auth()->id(),
|
||||||
'updated_by' => auth()->id(),
|
'updated_by' => auth()->id(),
|
||||||
'created_at' => now(),
|
'created_at' => now(),
|
||||||
'updated_at' => now(),
|
'updated_at' => now(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($tanamanHasPestDiseaseColumns) {
|
||||||
|
$row['jenis_hama'] = json_encode($autoPestDiseaseData['jenis_hama'] ?? []);
|
||||||
|
$row['jenis_penyakit'] = json_encode($autoPestDiseaseData['jenis_penyakit'] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $row;
|
||||||
});
|
});
|
||||||
|
|
||||||
$data->chunk(500)->each(function ($chunk) {
|
$data->chunk(500)->each(function ($chunk) {
|
||||||
@@ -329,7 +348,14 @@ class PetaniLahanController extends Controller
|
|||||||
->toArray()
|
->toArray()
|
||||||
]);
|
]);
|
||||||
$validated = $this->validateLahan($request);
|
$validated = $this->validateLahan($request);
|
||||||
$validated = array_merge($validated, $this->autoPestDiseaseData($petaniId));
|
$autoPestDiseaseData = $this->autoPestDiseaseData($petaniId);
|
||||||
|
$lahanHasPestDiseaseColumns = $this->hasPestDiseaseColumns((new Lahan)->getTable());
|
||||||
|
$tanamanHasPestDiseaseColumns = $this->hasPestDiseaseColumns((new Tanaman)->getTable());
|
||||||
|
|
||||||
|
if ($lahanHasPestDiseaseColumns) {
|
||||||
|
$validated = array_merge($validated, $autoPestDiseaseData);
|
||||||
|
}
|
||||||
|
|
||||||
$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();
|
||||||
$lahanCount = Lahan::where('petani_id', $petaniId)->count();
|
$lahanCount = Lahan::where('petani_id', $petaniId)->count();
|
||||||
@@ -382,8 +408,8 @@ class PetaniLahanController extends Controller
|
|||||||
$this->syncTanamanPenaung($request);
|
$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, $autoPestDiseaseData, $tanamanHasPestDiseaseColumns) {
|
||||||
return [
|
$row = [
|
||||||
'lahan_id' => $lahan->id,
|
'lahan_id' => $lahan->id,
|
||||||
'komoditas_id' => $lahan->komoditas_id,
|
'komoditas_id' => $lahan->komoditas_id,
|
||||||
'jenis_komoditas_id' => $lahan->jenis_komoditas_id,
|
'jenis_komoditas_id' => $lahan->jenis_komoditas_id,
|
||||||
@@ -396,13 +422,18 @@ class PetaniLahanController extends Controller
|
|||||||
'serangan_hama' => $lahan->serangan_hama,
|
'serangan_hama' => $lahan->serangan_hama,
|
||||||
'penyakit_tanaman' => $lahan->penyakit_tanaman,
|
'penyakit_tanaman' => $lahan->penyakit_tanaman,
|
||||||
'keparahan_serangan' => $lahan->keparahan_serangan,
|
'keparahan_serangan' => $lahan->keparahan_serangan,
|
||||||
'jenis_hama' => json_encode($lahan->jenis_hama ?? []),
|
|
||||||
'jenis_penyakit' => json_encode($lahan->jenis_penyakit ?? []),
|
|
||||||
'created_by' => auth()->id(),
|
'created_by' => auth()->id(),
|
||||||
'updated_by' => auth()->id(),
|
'updated_by' => auth()->id(),
|
||||||
'created_at' => now(),
|
'created_at' => now(),
|
||||||
'updated_at' => now(),
|
'updated_at' => now(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($tanamanHasPestDiseaseColumns) {
|
||||||
|
$row['jenis_hama'] = json_encode($autoPestDiseaseData['jenis_hama'] ?? []);
|
||||||
|
$row['jenis_penyakit'] = json_encode($autoPestDiseaseData['jenis_penyakit'] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $row;
|
||||||
});
|
});
|
||||||
|
|
||||||
$data->chunk(500)->each(function ($chunk) {
|
$data->chunk(500)->each(function ($chunk) {
|
||||||
@@ -410,11 +441,13 @@ class PetaniLahanController extends Controller
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$lahan->tanaman()->update([
|
if ($tanamanHasPestDiseaseColumns) {
|
||||||
'jenis_hama' => json_encode($lahan->jenis_hama ?? []),
|
$lahan->tanaman()->update([
|
||||||
'jenis_penyakit' => json_encode($lahan->jenis_penyakit ?? []),
|
'jenis_hama' => json_encode($autoPestDiseaseData['jenis_hama'] ?? []),
|
||||||
'updated_by' => auth()->id(),
|
'jenis_penyakit' => json_encode($autoPestDiseaseData['jenis_penyakit'] ?? []),
|
||||||
]);
|
'updated_by' => auth()->id(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
if ($request->ajax()) {
|
if ($request->ajax()) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
use App\Models\Pertanian\Petani;
|
use App\Models\Pertanian\Petani;
|
||||||
@@ -100,21 +101,27 @@ class PraktikBudidaya extends Model
|
|||||||
$jenisHama = $praktikBudidaya->jenis_hama ?? [];
|
$jenisHama = $praktikBudidaya->jenis_hama ?? [];
|
||||||
$jenisPenyakit = $praktikBudidaya->jenis_penyakit ?? [];
|
$jenisPenyakit = $praktikBudidaya->jenis_penyakit ?? [];
|
||||||
|
|
||||||
Lahan::where('petani_id', $praktikBudidaya->petani_id)->get()->each(function (Lahan $lahan) use ($jenisHama, $jenisPenyakit, $praktikBudidaya) {
|
if (Schema::hasColumn((new Lahan)->getTable(), 'jenis_hama')
|
||||||
$lahan->update([
|
&& Schema::hasColumn((new Lahan)->getTable(), 'jenis_penyakit')) {
|
||||||
'jenis_hama' => $jenisHama,
|
Lahan::where('petani_id', $praktikBudidaya->petani_id)->get()->each(function (Lahan $lahan) use ($jenisHama, $jenisPenyakit, $praktikBudidaya) {
|
||||||
'jenis_penyakit' => $jenisPenyakit,
|
$lahan->update([
|
||||||
'updated_by' => $praktikBudidaya->updated_by,
|
'jenis_hama' => $jenisHama,
|
||||||
]);
|
'jenis_penyakit' => $jenisPenyakit,
|
||||||
});
|
'updated_by' => $praktikBudidaya->updated_by,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Tanaman::where('petani_id', $praktikBudidaya->petani_id)->get()->each(function (Tanaman $tanaman) use ($jenisHama, $jenisPenyakit, $praktikBudidaya) {
|
if (Schema::hasColumn((new Tanaman)->getTable(), 'jenis_hama')
|
||||||
$tanaman->update([
|
&& Schema::hasColumn((new Tanaman)->getTable(), 'jenis_penyakit')) {
|
||||||
'jenis_hama' => $jenisHama,
|
Tanaman::where('petani_id', $praktikBudidaya->petani_id)->get()->each(function (Tanaman $tanaman) use ($jenisHama, $jenisPenyakit, $praktikBudidaya) {
|
||||||
'jenis_penyakit' => $jenisPenyakit,
|
$tanaman->update([
|
||||||
'updated_by' => $praktikBudidaya->updated_by,
|
'jenis_hama' => $jenisHama,
|
||||||
]);
|
'jenis_penyakit' => $jenisPenyakit,
|
||||||
});
|
'updated_by' => $praktikBudidaya->updated_by,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if ($kebiasaan == 0) {
|
if ($kebiasaan == 0) {
|
||||||
$skor_konservasi_tanah = 1;
|
$skor_konservasi_tanah = 1;
|
||||||
|
|||||||
@@ -12,26 +12,46 @@ return new class extends Migration
|
|||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
Schema::table('map_lahan', function (Blueprint $table) {
|
Schema::table('map_lahan', function (Blueprint $table) {
|
||||||
$table->jsonb('jenis_hama')->nullable();
|
if (! Schema::hasColumn('map_lahan', 'jenis_hama')) {
|
||||||
$table->jsonb('jenis_penyakit')->nullable();
|
$table->jsonb('jenis_hama')->nullable();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! Schema::hasColumn('map_lahan', 'jenis_penyakit')) {
|
||||||
|
$table->jsonb('jenis_penyakit')->nullable();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Schema::table('map_tanaman', function (Blueprint $table) {
|
Schema::table('map_tanaman', function (Blueprint $table) {
|
||||||
$table->jsonb('jenis_hama')->nullable();
|
if (! Schema::hasColumn('map_tanaman', 'jenis_hama')) {
|
||||||
$table->jsonb('jenis_penyakit')->nullable();
|
$table->jsonb('jenis_hama')->nullable();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! Schema::hasColumn('map_tanaman', 'jenis_penyakit')) {
|
||||||
|
$table->jsonb('jenis_penyakit')->nullable();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
Schema::table('map_lahan', function (Blueprint $table) {
|
Schema::table('map_lahan', function (Blueprint $table) {
|
||||||
$table->dropColumn('jenis_hama');
|
if (Schema::hasColumn('map_lahan', 'jenis_hama')) {
|
||||||
$table->dropColumn('jenis_penyakit');
|
$table->dropColumn('jenis_hama');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Schema::hasColumn('map_lahan', 'jenis_penyakit')) {
|
||||||
|
$table->dropColumn('jenis_penyakit');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Schema::table('map_tanaman', function (Blueprint $table) {
|
Schema::table('map_tanaman', function (Blueprint $table) {
|
||||||
$table->dropColumn('jenis_hama');
|
if (Schema::hasColumn('map_tanaman', 'jenis_hama')) {
|
||||||
$table->dropColumn('jenis_penyakit');
|
$table->dropColumn('jenis_hama');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Schema::hasColumn('map_tanaman', 'jenis_penyakit')) {
|
||||||
|
$table->dropColumn('jenis_penyakit');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user