lundi 20 juillet 2020

Laravel Observers retrieved amount passed even if not specified while updating collection

Laravel Version: 6.0

PHP Version:7.4

Database Driver & Version:

MySQL

Description:

Observers retrived amount passed while updating a collection even if it's not specified in update data array. Retrived method from Observer file of order item:

public function retrieved(OrderItem $orderItem)
    {
        if($orderItem->order){
            $currency_id = $orderItem->order->currency_id;
        }else{
            $currency_id = $orderItem->currency_id;
        }
        $orderItem->price = convertCurrency($currency_id, session('currency'),$orderItem->price); 
    }

This is the file where I have update method in which I have to forcefully specify getOriginal so that my existing data wont change.

if(in_array($status,array('placed','canceled','returned','delivered'))) {
 $order->items->whereNotIn('status', [OrderItem::$status['canceled']['code'], OrderItem::$status['returned']['code']])->each(function ($item) use ($status) {
 $item->update([
    'status' => OrderItem::$status[$status]['code'],
    'price' => $item->getOriginal()['price'] //if i wont pass this manually it overwrites my existing price with converted price
   ]);
});
}

Isn't there any way I can do it without manually forcing it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire