lahan dan tanaman inspeksi update

This commit is contained in:
2026-04-21 13:59:01 +07:00
parent 224d8754c8
commit cd0b755f46
5 changed files with 189 additions and 19 deletions
@@ -0,0 +1,142 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Map\InsLahan;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Carbon\Carbon;
class InspeksiLahanController extends Controller
{
public function index(Request $request): JsonResponse
{
$size = $request->integer('size', 10);
$query = InsLahan::query()->with(['lahan']);
if ($request->filled('search')) {
$s = strtolower($request->search);
$query->where(function($q) use ($s) {
$q->whereRaw('lower(notes) like (?)', ["%{$s}%"])
->orWhereRaw('lower(plant_cond) like (?)', ["%{$s}%"]);
});
}
if ($request->filled('sort')) {
$sort = $request->sort;
$direction = str_starts_with($sort, '-') ? 'desc' : 'asc';
$column = ltrim($sort, '-');
$query->orderBy($column, $direction);
} else {
$query->latest();
}
return response()->json($query->paginate($size));
}
public function store(Request $request): JsonResponse
{
Gate::authorize('petani');
$validated = $request->validate([
'petani_id' => 'required|exists:users,id',
'lahan_id' => 'required|exists:lahan,id',
'air' => 'nullable|string',
'ph' => 'nullable|numeric|between:0,14',
'humidity' => 'nullable|string',
'temperature' => 'nullable|string',
'plant_cond' => 'nullable|string',
'notes' => 'nullable|string',
'foto_url' => 'nullable|string',
'sync_status' => 'nullable|string',
]);
$today = Carbon::today();
$inspeksi = InsLahan::updateOrCreate(
[
'petani_id' => $validated['petani_id'],
'lahan_id' => $validated['lahan_id'],
'created_at' => InsLahan::where('lahan_id', $validated['lahan_id'])
->whereDate('created_at', $today)
->exists() ? InsLahan::whereDate('created_at', $today)->first()->created_at : now()
],
$validated
);
return response()->json([
'message' => $inspeksi->wasRecentlyCreated ? 'Inspeksi baru berhasil dibuat' : 'Inspeksi hari ini berhasil diperbarui',
'data' => $inspeksi
], $inspeksi->wasRecentlyCreated ? 201 : 200);
}
public function show(string $id): JsonResponse
{
$inspeksi = InsLahan::with('lahan')->findOrFail($id);
return response()->json($inspeksi);
}
public function update(Request $request, string $id): JsonResponse
{
Gate::authorize('petani');
$inspeksi = InsLahan::findOrFail($id);
$validated = $request->validate([
'air' => 'nullable|string',
'ph' => 'nullable|numeric|between:0,14',
'plant_cond' => 'nullable|string',
'notes' => 'nullable|string',
'foto_url' => 'nullable|string',
]);
$inspeksi->update($validated);
return response()->json($inspeksi);
}
public function destroy(string $id): JsonResponse
{
Gate::authorize('petani');
InsLahan::findOrFail($id)->delete();
return response()->json(null, 204);
}
public function batchUpsert(Request $request): JsonResponse
{
Gate::authorize('petani');
$request->validate([
'inspections' => 'required|array',
'inspections.*.lahan_id' => 'required',
'inspections.*.petani_id' => 'required',
]);
$dataToUpsert = [];
foreach ($request->inspections as $item) {
$dataToUpsert[] = [
"petani_id" => $item['petani_id'],
"lahan_id" => $item['lahan_id'],
"air" => $item['air'] ?? null,
"ph" => $item['ph'] ?? null,
"humidity" => $item['humidity'] ?? null,
"temperature" => $item['temperature'] ?? null,
"plant_cond" => $item['plant_cond'] ?? null,
"notes" => $item['notes'] ?? null,
"foto_url" => $item['foto_url'] ?? null,
"sync_status" => 'synced',
"created_at" => $item['created_at'] ?? now(),
"updated_at" => now(),
];
}
InsLahan::upsert($dataToUpsert, ['id'], [
'air', 'ph', 'humidity', 'temperature', 'plant_cond', 'notes', 'foto_url', 'sync_status', 'updated_at'
]);
return response()->json(['message' => 'Sinkronisasi batch berhasil'], 200);
}
}
@@ -8,7 +8,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
class InspeksiController extends Controller{
class InspeksiTanamanController extends Controller{
public function index(Request $request): JsonResponse
{
$size = +$request->get('size') ?: 10;
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace App\Models\Map;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class InsLahan extends Model
{
use HasFactory;
protected $table = 'inspeksi_lahan';
protected $fillable = [
'petani_id',
'lahan_id',
'kadar_air',
'ph',
'kelembapan',
'temperatur',
'kondisi_tanaman',
'catatan',
'foto_url',
'sync_status',
'created_at',
'updated_at'
];
protected $casts = [
'ph' => 'float',
'temperatur' => 'float',
'kelembapan' => 'float',
'created_at' => 'datetime',
];
public $timestamps = true;
public function lahan() {
return $this->belongsTo(\App\Models\Map\Lahan::class, 'lahan_id');
}
public function petani() {
return $this->belongsTo(\App\Models\User::class, 'petani_id');
}
}
+1 -17
View File
@@ -27,10 +27,7 @@ class Panen extends Model
'notes',
'sync_status',
];
/**
* Casting tipe data agar konsisten saat diakses di Frontend/JSON.
*/
protected $casts = [
'weight_kg' => 'float',
'harvest_date' => 'date:Y-m-d',
@@ -40,29 +37,16 @@ class Panen extends Model
'updated_at' => 'datetime',
];
/**
* Relasi ke Lahan (Many-to-One)
*/
public function lahan(): BelongsTo
{
return $this->belongsTo(Lahan::class, 'land_id');
}
/**
* Relasi ke Tanaman (Many-to-One)
*/
public function tanaman(): BelongsTo
{
// Pastikan nama model Tanaman sudah sesuai (misal: Tanaman atau Plant)
return $this->belongsTo(Tanaman::class, 'plant_id');
}
/**
* Relasi ke Profile Petani (Many-to-One)
*/
public function petani(): BelongsTo
{
// farmer_id merujuk ke tabel profiles (profile_id)
return $this->belongsTo(\App\Models\Profile::class, 'farmer_id');
}
}