mercredi 23 octobre 2019

Laravel Nova - Updating Model with Attribute Accessor

I have a Product model, on this model I have a custom getPriceAttribute accessor that returns Money object.

public function getPriceAttribute($price): Money
{
    return new Money($price, new Currency($this->currency_code));
}

In nova I want to update the price of a product in a field.

Text::make('Price', 'price')
    ->displayUsing(function ($price) {
        return $price->format();
    })
    ->resolveUsing(function ($price) {
        return $price->money->getAmount() / 100;
    })->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
        $value = $request[$requestAttribute] * 100;
        $model->{$attribute} = $value;
    }),

When trying to update a product in Nova I get an error staying that currency code should be a string. This is because $this->currency_code is NULL in the first code snippet but I do not know why.

Just to note, the price is displayed properly on index and detail view.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire