Home > database >  Indirect modification of overloaded element of App\Models\ has no effect
Indirect modification of overloaded element of App\Models\ has no effect

Time:01-18

I’m getting this error: Indirect modification of overloaded element of App\Models\ 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 = Midtrans\Snap::getSnapToken($transaction);
        $transaction->snap_token = $snapToken;
        $transaction->save();

anyone could help me to fix this?

CodePudding user response:

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 = Midtrans\Snap::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.

CodePudding user response:

Property [detail] does not exist on this collection instantly, when I tried to loop $penjualan->detail as $item, so I’m guessing the problem lays when I pass the data to penjualan controller.


public function detail()
{
  return $this->belongsToMany(PenjualanDetail::class,‘id_penjualan_detail, ‘id_penjualan_detail);
}


public function getSnapToken()
    { 
        $id          = session('id_penjualan');
        $penjualan   = Penjualan::query('penjualan_detail')->with('produk')->where('id_penjualan', $id)->get();
        $transaction = array();

        foreach ( $penjualan->detail as $item ) {
            $item_details  = [
                'id'       => $item->produk->kode_produk,
                'price'    => $item->jumlah * $item->subtotal,
                'quantity' => $item->jumlah,
                'name'     => $item->produk->nama_produk,
            ];
        }

        $transaction_details = [
            'order_id'     => $this->penjualan->id_penjualan,
            'gross_amount' => $this->penjualan->bayar,
        ];
    
        $customer_details  = [
            'first_name' => \App\Models\Penjualan::user,
            'last_name'  => \App\Models\Penjualan::user,
            'email'      => \App\Models\Penjualan::user,
            'phone'      => \App\Models\Penjualan::user,            
        ];
    
        $transaction = [
            'transaction_details'  => $transaction_details,
            'item_details'         => $item_details,
            'customer_details'     => $customer_details,
        ];

        $snapToken = \Midtrans\Snap::getSnapToken($transaction);
        $snapToken = Penjualan::where()->update(['snap_token' => $snapToken]);
       
        return $snapToken;
    }

 public function payment(Request $request, Penjualan $penjualan)
    {
        $snapToken = $penjualan->snap_token;
            if (is_null($snapToken)) {
                // Jika snap token masih NULL, buat token snap dan simpan ke database
    
                $midtrans  = new CreateSnapTokenService($penjualan);
                $snapToken = $midtrans->getSnapToken();
    
                $penjualan->snap_token = $snapToken;
                $penjualan->save();
            }

        return view('penjualan.payment', ['snap_token'=>$snapToken]);
    }

CodePudding user response:

edit

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

error

Midtrans API is returning API error. HTTP status code: 400 API response: {"error_messages":["transaction_details.gross_amount is required","transaction_details.gross_amount is not a number","transaction_details.order_id is required”]} ```
  • Related