feature: crud spatial planning and fix search in tourism and umkm

This commit is contained in:
2025-02-20 16:16:06 +07:00
parent dd331b4a08
commit 33b7131c33
25 changed files with 1214 additions and 14 deletions

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Imports;
use App\Models\SpatialPlanning;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Facades\DB;
use DateTime;
use Carbon\Carbon;
class SpatialPlanningImport implements ToCollection
{
protected static $processed = false;
/**
* Process each row in the file
*/
public function collection(Collection $rows)
{
if (self::$processed) {
return;
}
self::$processed = true;
if ($rows->isEmpty())
{
return;
}
//cari header secara otomatis
$header = $rows->first();
$headerIndex = collect($header)->search(fn($value) => !empty($value));
// Pastikan header ditemukan
if ($headerIndex === false) {
return;
}
foreach ($rows->skip(1) as $row) {
$dateValue = $row[7];
try {
// Coba parsing tanggal secara otomatis
$parsedDate = Carbon::parse($dateValue)->format('Y-m-d H:i:s');
} catch (\Exception $e) {
// Jika gagal parsing, atur nilai default atau null
$parsedDate = null;
}
$dataToInsert[] = [
'name'=>$row[1],
'kbli'=>$row[2],
'activities'=>$row[3],
'area'=>$row[4],
'location'=>$row[5],
'number'=>$row[6],
'date'=>$parsedDate,
];
}
if(!empty($dataToInsert)) {
SpatialPlanning::insert($dataToInsert);
} else {
return;
}
}
}