0

I am using laravel framework and trying to create a conditional calculation form. I have 2 inputs for adult numbers and children numbers

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

Also I have 3 inputs where adult price, children price and total price needs to be shown. And finally there is one input to apply a discount.

<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 ">

My Controller file looks like this

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;  }

My Blade file looks like this

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

I get these prices from a database

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

When I type 1 in the adults field it shows 100 when I type 2 it shows 200 in adult price input. But I am not sure how to show different price in children price field and how to show the the total in the total price text input nor apply a discount by entering a rebate percent in the discount field. For example if I type "10" in discount field it should show $90 in adult price as it will apply 10% discount. I am new to programming. I would appreciate any help.

Johnny
  • 1,685
  • 6
  • 24
  • 42
  • 1
    Which version of laravel you are using? In 5.0, pluck() meant select 1 field from a row. Then in 5.1, they removed pluck() and replaced it with value(). Then in 5.2, they replace lists(), which returns the whole column, with pluck() – Aman Rawat May 03 '17 at 08:20
  • oh! I am using 5.1 when I type echo $nPriceAdult and type something in the adults input it works. returns me the adult price from the database. – Johnny May 03 '17 at 08:24
  • 1
    Check this link http://stackoverflow.com/questions/34405138/laravel-5-2-pluck-method-returns-array – Aman Rawat May 03 '17 at 08:25
  • I have to correct my self I just found out that I am using v5.3.29 – Johnny May 03 '17 at 08:32

2 Answers2

2

What are the results of these 2 lines? My guess is they're probably returning a Collection, since you do a calculation with them later on which requires integers

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

According to the Laravel Query Builder docs (https://laravel.com/docs/5.3/queries#retrieving-results) you need to use ->value('email') to return a single value from a query. So it would become:

$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceAdult');
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceChild');
René
  • 662
  • 1
  • 4
  • 16
  • $nPriceAdult returns 100 and $nPriceChild returns 50 – Johnny May 03 '17 at 08:25
  • Are you sure? You said you use laravel 5.1 and according to the docs at https://laravel.com/docs/5.1/collections#method-pluck the pluck method returns an array in laravel 5.1, I think you need the get method: https://laravel.com/docs/5.1/collections#method-get – René May 03 '17 at 08:28
  • Sorry. I just checked php artisan --version returned me this Laravel Framework version 5.3.29 – Johnny May 03 '17 at 08:31
  • ok, I also sent you wrong docs, since that is about collections. You need Query Builder information: https://laravel.com/docs/5.3/queries#retrieving-results If you use `->value('nPriceAdult')` at the end instead of pluck, it should return one value – René May 03 '17 at 08:32
  • Great! It works. Thank you. When I type 1 in the adults field it shows 100 when I type 2 it shows 200 in adult price input. Can you help me with my other question. How can I do this for the children field and how can I apply a discount when I type 10 in discount field it should show 90 as it will apply 10% discount. – Johnny May 03 '17 at 08:37
  • I don't know exactly what you mean, but I would start by returning a json response from the controller (https://laravel.com/docs/5.3/responses#json-responses). On the success callback function of your ajax call you can retrieve these json properties. – René May 03 '17 at 08:41
  • So, `echo $msg = $nPriceAdult * $nAdults;` returns me the price of the total adults when I use `$nPriceAdult.val(msg);` As I said before when I type 1 in the adults input it shows $100 when I type 2 it shows $200 in adult price input. But I don't know how to return $50 in children price input using `$nPriceChild.val(msg);` when 1 is typed in nChildren input field. Sorry couldn't understand the documentation for responses it doesn't provides enough examples. – Johnny May 03 '17 at 08:55
  • Look into json responses, you can do something like this at the end of your controller: `return response()->json(['priceAdults' =>$nPriceAdult * $nAdults, 'priceChildren' => $nChildren * $nPriceChild]);` It will make these `priceAdult` and `priceChildren` variables accessible in your AJAX success callback: `fucntion(data) {console.log(data.priceAdult); console.log(data.priceChildren));` – René May 03 '17 at 08:57
0

Change these lines:

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

as below:

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

I think you got my point. While plucking the values from DB query it was returning a collection object. Which cannot be used directly in a equation where integer or number is required. So, instead of plucking the value just use the first method to get the single row as object instead of getting collection. Then use that objects value.

Imran
  • 4,582
  • 2
  • 18
  • 37
  • Hi Imran! Thanks for the reply. I got this part and updated my post. I am trying to return multiple responses and show them in the fields. Would you able to help me with this. I don't have an idea how to go from here. – Johnny May 03 '17 at 09:05