Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dian_'s avatar
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);
    }
0 likes
11 replies
dian_'s avatar
Level 1

@Snapey

"Undefined array key "kode_produk\””

 */
    public function offsetGet($key): mixed
    {
        return $this->items[$key];
    }

    /**
Sinnbeck's avatar

@dian_ what line in your own code. Select the correct file in the stack trace to the left

dian_'s avatar
Level 1

@Sinnbeck $row['kode_produk'] = ''. $item->produk['kode_produk'] .'</span';

Sinnbeck's avatar

@dian_ try doing this instead

$row['kode_produk'] = ''. $item->produk->kode_produk .'</span'; 
dian_'s avatar
Level 1

@Sinnbeck another error : Property [kode_produk] does not exist on this collection instance.

dian_'s avatar
Level 1

@Sinnbeck true, inside model detail.php.

public function produk()
    {
        return $this->hasMany(Produk::class, 'id_produk', 'id_produk');
    }
Sinnbeck's avatar
Sinnbeck
Best Answer
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.