简体   繁体   English

如何在 Laravel Nova 中获取两个文本字段的总和并设置为另一个文本字段

[英]How to get sum of two text fields and set to another text field in Laravel Nova

How can I get the sum of two text fields and set it to another text field when changing the value in Laravel Nova?更改 Laravel Nova 中的值时,如何获取两个文本字段的总和并将其设置为另一个文本字段? I want to get Quantity x Unit_Price and set it to Amount.我想获得 Quantity x Unit_Price 并将其设置为 Amount。

SimpleRepeatable::make('Purchase Order Items', 'purchase_order_items', [
    Select::make('Purchase_Order_Id')
        ->options(\App\Models\PurchaseOrder::pluck('serial_number', 'id'))
        ->displayUsingLabels()->rules('required'),
    Select::make('Product_id')->options(\App\Models\Product::pluck('product_name', 'id'))
        ->displayUsingLabels()->rules('required'),
    Text::make('Quantity'),
    Text::make('Unit_Price'),
    Text::make('Amount'),
    Select::make('Variant_id')
        ->options(\App\Models\Variant::pluck('productName', 'id'))
        ->displayUsingLabels()->rules('required'),
])

hi maybe i understood it wrong, but you can delete amount from database and make atribute amount through accessor and then use it on your form and you can hide it from create and update嗨,也许我理解错了,但是您可以从数据库中删除金额并通过访问器制作属性金额,然后在您的表单上使用它,您可以将其隐藏在创建和更新中

protected function amount(): Attribute
{
    return Attribute::make(
        get: fn() => $this->quantity*$this->unit_price,
    );
}

hope it helps希望能帮助到你

You can try this instead你可以试试这个


SimpleRepeatable::make('Purchase Order Items', 'purchase_order_items', [
    Select::make('Purchase_Order_Id')
        ->options(\App\Models\PurchaseOrder::pluck('serial_number', 'id'))
        ->displayUsingLabels()->rules('required'),
    Select::make('Product_id')->options(\App\Models\Product::pluck('product_name', 'id'))
        ->displayUsingLabels()->rules('required'),

    Number::make('Quantity'),
    Number::make('Unit_Price'),
    Text::make('Amount', function ($model) {
        return $model->quantity + $model->unit_price;
    }),

    Select::make('Variant_id')
        ->options(\App\Models\Variant::pluck('productName', 'id'))
        ->displayUsingLabels()->rules('required'),
])

As you can see Text fields are replaced with Number fields so that you can sum 2 fields and show the result in computed Amount field.如您所见,文本字段已替换为数字字段,以便您可以对 2 个字段求和并在计算的金额字段中显示结果。

You can take a look at computed fields definition.您可以查看计算字段定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM