根据Laravel中的复选框选择,将字段值提交给数据库

时间:2017-12-14 18:36:11

标签: php laravel laravel-5 controller

我有一个项目,我有一个字段,当前将其内容添加到我的数据库中的一个字段,名称为Total。我想做的是能够将输入提交到两个字段(这很容易),但在极少数情况下,我希望它将第二个字段(余额)默认为0。

所以,目前,这是我控制器的一部分:

    $shipment->noCharge = request('noCharge');
    $shipment->noSettle = request('noSettle');
    $shipment->Total = request('Total');

我希望能够做到这样的事情:

    $shipment->noCharge = request('noCharge');
    $shipment->noSettle = request('noSettle');
    $shipment->Total = request('Total');
    $shipment->Balance = 
      //if (request('noCharge') = 1)
      // 0.00
      //else if (request('noSettle') = 1)
      // 0.00
      //else
       request('Total');
      //

显然我上面的工作没有成功,但我希望它能正确描绘我想要做的事情。

2 个答案:

答案 0 :(得分:1)

为什么不简单地使用一个变量

$shipment->noCharge = request('noCharge');
$shipment->noSettle = request('noSettle');
$shipment->Total = request('Total');
if ((request('noCharge') == 1) || (request('noSettle') == 1)){
   $balance = 0.00;
}else{
   $balance = request('Total');
}
$shipment->Balance = $balance;

OR与三元运算符

答案 1 :(得分:0)

你是否暗示这样的事情?

 $shipment->noCharge = request('noCharge');
    $shipment->noSettle = request('noSettle');
    $shipment->Total = request('Total');
      if (request('noCharge') == 1) {
        $shipment->Balance = 0.00;
      }else if (request('noSettle') == 1) {
        $shipment->Balance = 0.00; 
      }else {
        $shipment->Balance = request('Total');
      }