我有一个项目,我有一个字段,当前将其内容添加到我的数据库中的一个字段,名称为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');
//
显然我上面的工作没有成功,但我希望它能正确描绘我想要做的事情。
答案 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');
}