簡體   English   中英

如何在 Laravel Nova 中獲取兩個文本字段的總和並設置為另一個文本字段

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

更改 Laravel Nova 中的值時,如何獲取兩個文本字段的總和並將其設置為另一個文本字段? 我想獲得 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'),
])

嗨,也許我理解錯了,但是您可以從數據庫中刪除金額並通過訪問器制作屬性金額,然后在您的表單上使用它,您可以將其隱藏在創建和更新中

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

希望能幫助到你

你可以試試這個


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'),
])

如您所見,文本字段已替換為數字字段,以便您可以對 2 個字段求和並在計算的金額字段中顯示結果。

您可以查看計算字段定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM