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.

4
  • 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() Commented May 3, 2017 at 8: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. Commented May 3, 2017 at 8:24
  • 1
    Check this link stackoverflow.com/questions/34405138/… Commented May 3, 2017 at 8:25
  • I have to correct my self I just found out that I am using v5.3.29 Commented May 3, 2017 at 8:32

2 Answers 2

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');
Sign up to request clarification or add additional context in comments.

8 Comments

$nPriceAdult returns 100 and $nPriceChild returns 50
Are you sure? You said you use laravel 5.1 and according to the docs at 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: laravel.com/docs/5.1/collections#method-get
Sorry. I just checked php artisan --version returned me this Laravel Framework version 5.3.29
ok, I also sent you wrong docs, since that is about collections. You need Query Builder information: laravel.com/docs/5.3/queries#retrieving-results If you use ->value('nPriceAdult') at the end instead of pluck, it should return one value
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.
|
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.