我想在Laravel中创建一个更新方法,当我编辑表servis
时,使其在表servis_details
中有很多细节。
在编辑视图中,我可以:
如果删除项目,它将删除项目的stok
更新为表项目stok
。如果我在表servis_details
中添加了新项目,则会根据数量输入而减少stok。
我的问题是,我有一个Codeigniter语法/方法可以帮助我解决上述要求,但是我需要将该语法包含在Laravel控制器中,
有人可以帮我转换下面的代码吗?
function save_detil()
{
if ($this->input->post('adadata') > 0)
{
$dataold = $this->M_servis->getDetilService($this->input->post('id_service'));
$stockAkhirLama = [];
foreach ($dataold as $aValue) {
if ($aValue->status === '1')
{
$getSparepartLama = $this->M_sparepart->find($aValue->nama_id);
$stockLama[] = [
'id_sparepart' => $aValue->nama_id,
'jumlah_stok' => $getSparepartLama->jumlah_stok + $aValue->qty
];
$stockAkhirLama = [];
$stockAkhirLama = array_merge($stockAkhirLama, $stockLama);
}
}
$this->M_servis->update_sparepart_old($stockAkhirLama, $this->input->post('id_service'),
count($stockAkhirLama));
}
$stockAkhir = [];
foreach ($this->input->post('tbdetil') as $iValue) {
if ($iValue[8] === '1') {
$getSparepart = $this->M_sparepart->find($iValue[7]);
$stock[] = [
'id_sparepart' => $iValue[7],
'jumlah_stok' => $getSparepart->jumlah_stok - $iValue[2]
];
$stockAkhir = [];
$stockAkhir = array_merge($stockAkhir, $stock);
}
$dataDetil[] = [
'id_service' => $iValue[6],
'nama_id' => $iValue[7],
'nama_service' => $iValue[1],
'qty' => $iValue[2],
'harga' => $iValue[3],
'subtotal' => $iValue[4],
'status' => $iValue[8]
];
}
$this->M_servis->save_detil($stockAkhir, $dataDetil, count($stockAkhir));
echo json_encode(['success' => true, 'message' => 'Berhasil']);
}