簡體   English   中英

Laravel表單模型綁定中的復選框舊值

[英]Checkbox old value in Laravel form model binding

我有一個要使用“表單模型綁定”構建的表單(這對我來說是新的東西),並且遇到了由於某些原因不想與我一起使用的復選框。

我有兩個復選框可以轉到數據庫中的同一字段。 目前,我將它們格式化為:

{{ Form::checkbox('shipment_billing_status', '2', $shipment->shipment_billing_status) }}
{{ Form::checkbox('shipment_billing_status', '3', $shipment->shipment_billing_status) }}

這兩個字段都被命名並進入數據庫的“ shipment_billing_status”,默認值為2和3。但是我不確定如何從數據庫中獲取舊值。

目前,它們都顯示為已選中,但是鑒於我的“ shipment_billing_status”字段等於1,因此此時都不應選中它們。

如何解決此問題?

您可以向Form :: checkbox傳遞4個參數,第三個參數為true或false。 當您調用$ shipment-> shipment_billing_status時,它將返回數據庫中的任何值。 由於它的計算結果不為假,因此選中了該框。 我會像這樣在您的模型上放置一個增變器:

public function getCheckboxShipmentBillingStatusAttribute(){
    $bool = ($this->shipment_billing_status == 3)? true : false;
    return $bool;
}

然后調用它而不是$ shipment-> shipment_billing_status

{{ Form::checkbox('shipment_billing_status', '2', $shipment->checkbox_shipment_billing_status) }}

它將返回true並選中是否等於3的框,否則返回false並顯示為未選中。 表單模型綁定期望該字段將以true或false的形式存儲在數據庫中,並且不依賴於1或2或3。因此,除了對值進行變異之外,沒有簡單的方法可以在其中實現數字系統。

有關文檔,請參見https://laravel.com/docs/5.5/eloquent-mutators

暫無
暫無

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

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