update controller
This commit is contained in:
@@ -24,7 +24,7 @@ class AksesPasarController extends Controller
|
|||||||
{
|
{
|
||||||
$petani = Petani::findOrFail($petaniId);
|
$petani = Petani::findOrFail($petaniId);
|
||||||
|
|
||||||
$aksesPasar = AksesPasar::where('petani_id', $petaniId)->first();
|
$aksesPasar = AksesPasar::where('petani_id', $petaniId)->latest()->first();
|
||||||
$hargaJualPanen = HargaJualPanen::where('petani_id', $petaniId)->get();
|
$hargaJualPanen = HargaJualPanen::where('petani_id', $petaniId)->get();
|
||||||
$hargaJualRows = collect([
|
$hargaJualRows = collect([
|
||||||
'Buah segar hasil petik',
|
'Buah segar hasil petik',
|
||||||
@@ -50,32 +50,40 @@ class AksesPasarController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$validated = $this->validateData($request);
|
$validated = $this->validateData($request);
|
||||||
|
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$aksesPasarData = $this->aksesPasarPayload($validated);
|
$aksesPasarData = $this->aksesPasarPayload($validated);
|
||||||
|
|
||||||
|
$existingAksesPasar = AksesPasar::where('petani_id', $validated['petani_id'])->first();
|
||||||
|
|
||||||
|
if ($existingAksesPasar) {
|
||||||
|
$aksesPasarData['updated_by'] = Auth::id();
|
||||||
|
$existingAksesPasar->update($aksesPasarData);
|
||||||
|
$aksesPasar = $existingAksesPasar;
|
||||||
|
} else {
|
||||||
$aksesPasarData['uid'] = 'AKP-' . Str::uuid();
|
$aksesPasarData['uid'] = 'AKP-' . Str::uuid();
|
||||||
$aksesPasarData['created_by'] = Auth::id();
|
$aksesPasarData['created_by'] = Auth::id();
|
||||||
$aksesPasarData['updated_by'] = Auth::id();
|
$aksesPasarData['updated_by'] = Auth::id();
|
||||||
|
|
||||||
$aksesPasar = AksesPasar::create($aksesPasarData);
|
$aksesPasar = AksesPasar::create($aksesPasarData);
|
||||||
|
|
||||||
if ($request->has('harga_jual')) {
|
|
||||||
$this->syncHargaJual($request->petani_id, $validated['harga_jual'] ?? [], Auth::id());
|
|
||||||
}
|
|
||||||
|
|
||||||
DB::commit();
|
|
||||||
|
|
||||||
return redirect()->back()->with('success', 'Data Rantai Nilai dan Akses Pasar berhasil disimpan.');
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
DB::rollBack();
|
|
||||||
return redirect()->back()->withErrors(['error' => 'Gagal menyimpan data: ' . $e->getMessage()])->withInput();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($request->has('harga_jual')) {
|
||||||
|
$this->syncHargaJual($request->petani_id, $validated['harga_jual'] ?? [], Auth::id());
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
return redirect()->back()->with('success', 'Data Rantai Nilai dan Akses Pasar berhasil disimpan.');
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
DB::rollBack();
|
||||||
|
return redirect()->back()->withErrors(['error' => 'Gagal menyimpan data: ' . $e->getMessage()])->withInput();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function update(Request $request, $petaniId, $id)
|
public function update(Request $request, $petaniId, $id)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user