Laravel 中的条件形式计算

Conditional form calculation in Laravel

我正在使用 laravel 框架并尝试创建条件计算表单。我有 2 个输入成人号码和儿童号码

<input type="text" name="nAdults" id="nAdults" value="" class="form-control ">
<input type="text" name="nChildren" id="nChildren" value="" class="form-control ">

我还有 3 个输入,其中需要显示成人价格、儿童价格和总价。最后还有一个应用折扣的输入。

<input type="text" name="nPriceAdult" id="nPriceAdult" value="" class="form-control ">
<input type="text" name="nPriceChild" id="nPriceChild" value="" class="form-control ">
<input type="text" name="nTotalPrice" id="nTotalPrice" value="" class="form-control ">
<input type="text" name="nDiscount_percent" id="nDiscount_percent" value="" class="form-control ">

我的控制器文件是这样的

public function priceCalculator(Request $request)
{

$nAdults = $request->input('nAdults');
$nChildren = $request->input('nChildren');
$kfnTourID = $request->input('_kfnTourID');         
$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceAdult');
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceChild');

    echo $msg = $nPriceAdult * $nAdults;  }

我的 Blade 文件是这样的

var $nAdults = $('#nAdults'), $nPriceAdult = $('#nPriceAdult'), $nPriceChild = $('#nPriceChild');
            $nAdults.on('keyup', function(){

                        $.ajax({
                            type: 'POST',
                            data: $('#bookingsFormAjax').serialize(), 
                            url:'bookings/calculate',
                            dataType: 'json',
                            success: function(msg){
                                $nPriceAdult.val(msg); 
                                $nPriceChild.val(msg); 
                                console.log(msg); 
                            }
                        });
                    });

我从数据库中获取这些价格

------------------------------------
_kpnID nPriceAdult nPriceChild
------------------------------------
2         100           50
3         200           100

当我在成人字段中输入 1 时它显示 100 当我输入 2 时它在成人价格输入中显示 200。但我不确定如何在儿童价格字段中显示不同的价格以及如何在总价文本输入中显示总价,也不知道如何通过在折扣字段中输入折扣百分比来应用折扣。例如,如果我在折扣字段中输入“10”,它应该显示 90 美元的成人价格,因为它将应用 10% 的折扣。我是编程新手。如果有任何帮助,我将不胜感激。

这两行的结果是什么?我的猜测是他们可能 return 收集了一个集合,因为稍后你用它们进行计算需要整数

$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->pluck('nPriceAdult');
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->pluck('nPriceChild');

根据 Laravel 查询生成器文档 (https://laravel.com/docs/5.3/queries#retrieving-results),您需要使用 ->value('email') 到 return 来自查询的单个值。所以它会变成:

$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceAdult');
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceChild');

更改这些行:

$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->pluck('nPriceAdult');
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->pluck('nPriceChild');

如下:

$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->first()->nPriceAdult;
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->first()->nPriceChild;

我想你明白我的意思了。从数据库查询中提取值时,它返回一个集合对象。它不能直接用于需要整数或数字的方程式中。因此,与其提取值,不如使用第一种方法将单行作为对象而不是获取集合。然后使用该对象值。