update controller
This commit is contained in:
@@ -23,8 +23,8 @@ class AksesPasarController extends Controller
|
||||
public function index($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();
|
||||
$hargaJualRows = collect([
|
||||
'Buah segar hasil petik',
|
||||
@@ -50,32 +50,40 @@ class AksesPasarController extends Controller
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $this->validateData($request);
|
||||
{
|
||||
$validated = $this->validateData($request);
|
||||
|
||||
DB::beginTransaction();
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$aksesPasarData = $this->aksesPasarPayload($validated);
|
||||
try {
|
||||
$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['created_by'] = Auth::id();
|
||||
$aksesPasarData['updated_by'] = Auth::id();
|
||||
|
||||
$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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user