skip to Main Content

I’m getting this error: Indirect modification of overloaded element of AppModels has no effect, change ->get(); to ->first->toArray(); another error Call to a member function toArray() on null

here the code

$penjualan   = Penjualan::find(session('id_penjualan'));
$detail      = PenjualanDetail::with('produk')->where('id_penjualan', session('id_penjualan'))->get();
$transaction = new Penjualan();

        foreach ($detail as $item) {
            $transaction_details = [
                'order_id'     => $penjualan->id_penjualan,
                'gross_amount' => $penjualan->bayar,
            ];
    
            $item_details = [
                'id'       => $penjualan->id_penjualan,
                'price'    => $item->harga_jual,
                'quantity' => $item->jumlah,
                'name'     => $item->produk->nama_produk,
            ];
    
            $customer_details  = [
                'first_name' => $request->get('uname'),
                'last_name'  => '',
                'email'      => $request->get('email'),
                'phone'      => $request->get('number'),            
            ];
    
            $transaction = [
                'transaction_details'  => $transaction_details,
                'item_details'         => $item_details,
                'customer_details'     => $customer_details,
            ];
        }
 
        $snapToken = MidtransSnap::getSnapToken($transaction);
        $transaction->snap_token = $snapToken;
        $transaction->save();

anyone could help me to fix this?

2

Answers


  1. Chosen as BEST ANSWER
     public function getSnapToken ()
        { 
            $id             = session('id_penjualan');
            $member         = Member::orderBy('nama')->get();
            $penjualan      = Penjualan::with(['penjualan_detail' => $detail = function ($query) {
                                $query->where('item', 'kode_produk, subtotal, jumlah, nama_produk');
                                }])->where('id_penjualan', ('id_penjualan'))
                                ->orderBy('id_penjualan')
                                ->get();
            $transaction    = array($penjualan);
    
            foreach ( $detail as $item ) {
                $transaction_details = [
                    'order_id'     => $this->penjualan->id_penjualan,
                    'gross_amount' => $this->penjualan->bayar,
                ];
    
                $item_details  = [
                    'id'       => $item->produk->kode_produk,
                    'price'    => $item->jumlah * $item->subtotal,
                    'quantity' => $item->jumlah,
                    'name'     => $item->produk->nama_produk,
                ];
    
                $customer_details  = [
                    'id'         => $penjualan->member->kode_member,  
                    'first_name' => $validated['uname'],
                    'last_name'  => $validated['lname'],
                    'email'      => $validated['email'],
                    'phone'      => $validated['number'], 
                    // 'billing_address' => '',
                    // 'shipping_address' => '',          
                ];       
    
                $transaction = [
                    'transaction_details'  => $transaction_details,
                    'item_details'         => $item_details,
                    'customer_details'     => $customer_details,
                ];
            }
    
            $snap_token = '';
            try {
                $snap_token = MidtransSnap::getSnapToken($transaction);
            }
             catch (Exception $e) {
                echo $e->getMessage();
            } 
    
            echo "snap_token = ".$snap_token;
        }
    
      public function payment(Penjualan $penjualan)
        {
            $snap_token = $penjualan->snap_token;
                if (is_null($snap_token)) {
        
                    $midtrans   = new CreateSnapTokenService($penjualan);
                    $snap_token = $midtrans->getSnapToken();
        
                    $snap_token = Penjualan::where('id_penjualan')->update(['snap_token' => $snap_token]);
                }
    
            return view('penjualan.payment', ['snap_token'=>$snap_token]);
        }
    

  2. I have done Midtrans payment before.
    The problem you’ve got with your current code are

    • you want to store snap_token inside your Penjualan model, but you use new Penjualan()
    • when multiple items are ordered, your code do not support it cause $item_details is a single array inside foreach loop

    So here is an updated code with some notes and recommendations.

    $id = session('id_penjualan');
    
    $validated = $request->validate([
      'uname'   => 'required|string',
      // 'lname'    => 'nullable|string',
      'email'   => 'required|string',
      'number'  => 'required|string',
    ]);
    
    // use single query to retrieve Penjualan and PenjualanDetail
    // for product name, recommend to store it within PenjualanDetail, along with price and base_price
    $penjualan = Penjualan::query()
        ->with('details')
        ->find($id);
    
    if (!$penjualan) {
      // you can redirect your customer to cart or any other page
      return redirect('cart')->with('error', 'Can not find Penjualan.');
    }
    
    $item_details = [];
    
    foreach ($penjualan->details as $item) {
        $item_details[] = [
            'id'       => $item->id,
            'price'    => $item->harga_jual,
            'quantity' => $item->jumlah,
            // recommended product's name to be stored within PenjualanDetail
            'name'     => $item->nama_produk,
        ];
    }
    
    $transaction_details = [
        'order_id'      => $penjualan->id_penjualan,
        // "bayar" must not contain any decimal, use cast Money
        'gross_amount'  => $penjualan->bayar,
    ];
    
    // Optional
    $customer_details  = [
        'first_name' => $validated['uname'],
        'last_name'  => '', // $validated['lname'],
        'email'      => $validated['email'],
        'phone'      => $validated['number'],    
        // 'billing_address' => '',
        // 'shipping_address' => '',
    ];
    
    // this is a simple array that will be sent to method getSnapToken()
    $transaction = [
        'transaction_details'  => $transaction_details,
        'customer_details'     => $customer_details,
        'item_details'         => $item_details,
    ];
    
    // this method only accept good old array
    $snapToken = MidtransSnap::getSnapToken($transaction);
    
    $penjualan->snap_token = $snapToken;
    
    // recommend to store your customer detail here, either multiple fields or single array field storing $customer_details
    
    // $penjualan->first_name = $customer_details['first_name'];
    // $penjualan->last_name = $customer_details['last_name'];
    // $penjualan->email = $customer_details['email'];
    // $penjualan->phone = $customer_details['phone'];
    // $penjualan->billing_address = $customer_details['billing_address'];
    // $penjualan->shipping_address = $customer_details['shipping_address'];
    
    $penjualan->save();
    

    I commented out some parts that you might not use, where I did used them in the past.

    Feel free to adjust them to suite your needs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search