mercredi 7 novembre 2018

access Laravel model attributes along with updateOrCreate

so I would like to be able to access the model's quantity attribute while in the mutator method, but for some reason, no attributes are passed but sell_price. Since it will create a new OrderItem if none found, how can it not have these attributes?

model method:

public function setSellPriceAttribute($value)
{
    if ($value != null) {
        if($this->referral_type_id !== 3) {
            $this->attributes['sell_price'] = $value * $this->quantity;
        }
    } else {
        $price = Item::where(['id' => $this->item_id])->value('sell_price');
        $this->attributes['sell_price'] = $price * $this->quantity;
    }
}

usage:

 $orderItem = OrderItem::updateOrCreate(['order_id' => $orderId, 'item_id' => $existingItem->id],
                            [
                                'sell_price' => $itemData['sell_price'],
                                'quantity' => $itemData['quantity'],
                                'referral_id' => isset($itemData['referral_id']) ? $itemData['referral_id'] : null
                            ]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire