dimanche 22 août 2021

Trying to get property 'id_pembelian' of non-object

I want to retrieve data from the database without using foreach, in some articles I read just use first() to get the value from the database, but when I try I always get an error like the following: ErrorException Trying to get property 'id_belian' of non-object

can anyone help me?

this is my controller code :

 /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $isValid = $request->validate([
            'total_bayar' => 'required|integer'
        ]);


        // megambil total yang sudah di bayar
        $terbayar = Pembayaran::select('total_bayar')->where('id',$id)->first();

        // mengambil id pembelian dari tabel pembayaran
        $id_data_bayar = Pembayaran::select('id_pembelian')->where('id',$id)->first();

        // ambil data jumlah yang harus di bayar dari tabel pembelian
        $harga_bayar = Pembelian::select('total_harga')->where('id',$id_data_bayar->id_pembelian)->first();

        $total_akan_terbayar = $terbayar->total_bayar + $request->get('total_bayar');  

        if($isValid){
            if(($harga_bayar->total_harga != $terbayar->total_bayar) && ($harga_bayar->total_harga > $total_akan_terbayar)){
                $data = [
                    'total_bayar'=> $request->get('total_bayar'),
                    'updated_at' => Carbon::now(),
                    'status' => 'cicil'
                ];
                $isSaved = Pembayaran::where('id',$id)->update($data);
                if($isSaved){
                    return Redirect::to('/pembayaran/'.$id.'/edit')->with('warning','Transaksi Anda Sebesar Rp.'.$total_akan_terbayar.' Berhasil dicatat');
                }else{
                    return Redirect::to('/pembayaran/'.$id.'/edit')->with('error','Transaksi Anda Gagal dicatat');
                }
            } elseif(($harga_bayar->total_harga != $terbayar->total_bayar) && ($harga_bayar->total_harga == $total_akan_terbayar)) {
                $data = [
                    'total_bayar'=> $request->get('total_bayar'),
                    'updated_at' => Carbon::now(),
                    'status' => 'lunas'
                ];
                $isSaved = Pembayaran::where('id',$id)->update($data);
                if($isSaved){
                    return Redirect::to('/pembayaran/'.$id.'/edit')->with('warning','Transaksi Anda Sebesar Rp.'.$total_akan_terbayar.' Berhasil dicatat');
                }else{
                    return Redirect::to('/pembayaran/'.$id.'/edit')->with('error','Transaksi Anda Gagal dicatat');
                }
            } elseif(($harga_bayar->total_harga != $terbayar->total_bayar) && ($harga_bayar->total_harga < $total_akan_terbayar)) {
                return Redirect::to('/pembayaran/'.$id.'/edit')->with('warning','Jumlah yang anda masukan berlebih Rp.'.($total_akan_terbayar-$harga_bayar->total_harga));
            } else {
                return Redirect::to('/pembayaran/'.$id.'/edit')->with('error','Transaksi Anda Gagal dicatat');
            }
        }else{
            return Redirect::to('/pembayaran/'.$id.'/edit')->with('error','Transaksi Anda Gagal dicatat');
        }
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire