简体   繁体   中英

Submit value of field to database based on checkbox selection in Laravel

I have a project where I have one field that currently adds its contents to one field in my database with the name Total. What I would like to do is be able to submit the input to two fields (which is easy enough), but on the rare occasion, I would like it to default the second field (balance) to 0.

So, at the current moment, here is a portion of my controller:

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

I would like to be able to do something like:

    $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');
      //

Obviously my above won't work, but I hope it properly portrays what I am trying to do.

Why not simply do with one variable

$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 with ternary operator

are you hinting at something like this?

 $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');
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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