error message?
Jan 22, 2023
11
Level 1
How to fix undefined array key?
I have the following code in backend, and when I try retrieving the data from the data, undefined key error message appears. can someone help me to fix this?
public function data($id)
{
$detail = PenjualanDetail::with('produk')
->where('id_penjualan', $id)
->get();
$data = array();
$total = 0;
$total_item = 0;
foreach ($detail as $item) {
$row = array();
$row['kode_produk'] = '<span class="label label-success">'. $item->produk['kode_produk'] .'</span';
$row['nama_produk'] = $item->produk['nama_produk'];
$row['harga_jual'] = 'Rp. '. format_uang($item->harga_jual);
$row['jumlah'] = '<input type="number" class="form-control input-sm quantity" data-id="'. $item->id_penjualan_detail .'" value="'. $item->jumlah .'">';
$row['ppn'] = $item->ppn . '%';
$row['diskon'] = $item->diskon . '%';
$row['subtotal'] = 'Rp. '. format_uang($item->subtotal);
$row['aksi'] = '<div class="btn-group">
<button onclick="deleteData(`'. route('transaksi.destroy', $item->id_penjualan_detail) .'`)" class="btn btn-xs btn-danger btn-flat"><i class="fa fa-trash"></i></button>
</div>';
$data[] = $row;
$total += $item->harga_jual * $item->jumlah + (($item->ppn * $item->jumlah) / 100 * $item->harga_jual)
- (($item->diskon * $item->jumlah) / 100 * $item->harga_jual);;
$total_item += $item->jumlah;
}
$data[] = [
'kode_produk' => '
<div class="total hide">'. $total .'</div>
<div class="total_item hide">'. $total_item .'</div>',
'nama_produk' => '',
'harga_jual' => '',
'jumlah' => '',
'ppn' => '',
'diskon' => '',
'subtotal' => '',
'aksi' => '',
];
return datatables()
->of($data)
->addIndexColumn()
->rawColumns(['aksi', 'kode_produk', 'jumlah'])
->make(true);
}
Level 102
@dian_ you cannot grab a kode from all of them at once. It's say one PenjualanDetail has 10 Produk. Which kode should it use then?
Please or to participate in this conversation.