Skip to content
Advertisement

Update price column in database laravel

I am trying to pass along updated pricing to my orders table but am getting an error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'price' cannot be null (SQL: update `orders` set `status` = paid, `price` = ? where `order_id` = 66)

In my controller I have this:

    public function showPayment() {
    $cart = Session::get('cart');
    $payment_info = Session::get('payment_info');
    $total_weight = $cart->totalWeight;

    if($total_weight <= 16.00 ) {
         
         $sales_tax = $cart->totalPrice * .085 ;

         $payment_total = $cart->totalPrice + $sales_tax + 5;

        return view('cart.payments', 
            ['payment_info' => $payment_info, 'cartItems' => $cart, 'sales_tax' => $sales_tax, 'total_weight'=>$total_weight, 'payment_total' => $payment_total]
        );
        
    }else{
         $total_weight = $cart->totalWeight;
         $sales_tax = $cart->totalPrice * .085 ;

         $payment_total = $cart->totalPrice + $sales_tax + 10;

        return view('cart.payments', 
            ['payment_info' => $payment_info, 'cartItems' => $cart, 'sales_tax' => $sales_tax, 'total_weight'=>$total_weight, 'payment_total' => $payment_total]
        );
    }

    $newPaymentTotal = $payment_total;
    $request->session()->put('newPaymentTotal', $newPaymentTotal);
}

 private function storePaymentInfo($paypalPaymentID, $paypalPayerID){

       $payment_info = Session::get('payment_info');
       $order_id = $payment_info['order_id'];
       $status = $payment_info['status'];
       $paypal_payment_id = $paypalPaymentID;
       $paypal_payer_id = $paypalPayerID;
       $newPaymentTotal = Session::get('newPaymentTotal');


   if($status == 'on_hold'){
   
    //create (issue) a new payment row in payments table
        $date = date('Y-m-d H:i:s');
        $newPaymentArray = array("order_id"=>$order_id,"date"=>$date,"amount"=>$payment_info['price'],
            "paypal_payment_id"=>$paypal_payment_id, "paypal_payer_id" => $paypal_payer_id);

        $created_order = DB::table("payments")->insert($newPaymentArray);
       

   //update payment status in orders table to "paid"
   
   DB::table('orders')->where('order_id', $order_id)->update(array('status' => 'paid', 'price' => $newPaymentTotal));
   
  }

}

The error is coming from the $newPaymentTotal that I want use to update the price that was in there before. That column is set up as a decimal as well.

Advertisement

Answer

Your line $request->session()->put('newPaymentTotal', $newPaymentTotal); never gets hit due to this issue:

public function showPayment() {

    //... 
    if($total_weight <= 16.00 ) {

        //... 

        return view('cart.payments',
            ['payment_info' => $payment_info, 'cartItems' => $cart, 'sales_tax' => $sales_tax, 'total_weight'=>$total_weight, 'payment_total' => $payment_total]
        );

    } else{

        //... 
        return view('cart.payments',
            ['payment_info' => $payment_info, 'cartItems' => $cart, 'sales_tax' => $sales_tax, 'total_weight'=>$total_weight, 'payment_total' => $payment_total]
        );
    }

    $newPaymentTotal = $payment_total;  // note, this is a bit redundant
    $request->session()->put('newPaymentTotal', $newPaymentTotal); // this needs to happen BEFORE you return, at this point, its too late, this never gets called
}

In either case, you return a value before setting the session variable. You’ll need to move that to be before you call return

NOTE: Thats gonna cause a new error becuase $request isnt defined, you probably want request()->session()->put(.... there

That said, the whole method could be simplified using a ternary statement to something like:

public function showPayment() {
    $cart          = Session::get('cart');
    $payment_info  = Session::get('payment_info');
    $total_weight  = $cart->totalWeight;
    $addend        = $total_weight <= 16.00 ? 5 : 10;
    $sales_tax     = $cart->totalPrice * .085 ;
    $payment_total = $cart->totalPrice + $sales_tax + $addend;
    request()->session()->put('newPaymentTotal', $payment_total);

    return view('cart.payments',
        [
            'payment_info'  => $payment_info, 
            'cartItems'     => $cart, 
            'sales_tax'     => $sales_tax,
            'total_weight'  => $total_weight, 
            'payment_total' => $payment_total
        ]
    );
}

On a related but off topic note:

Be very careful doing math with floating point numbers in PHP. And by careful, I really mean DONT do it. You WILL run into issues that cause very difficult to understand and trace back bugs. Instead, I ALWAYS do math by first converting all floats to an integer value, doing all the math needed with those integers, then converting back to a float for display purposes at the end.

$totalPrice    = 124.25;
$addend        = 10;
$taxRate       = .085;

$intTotalPrice = $totalPrice * 1000; // 1000 becuase our tax rate goes to 3 places, our multiplier needs to have the same number of (or more) 0s as the max number of places our floats have
$intAddend     = $addend * 1000;
$intTaxRate    = $taxRate * 1000;


$intPrice1 = $price1 * 100;
$intPrice2 = $price2 * 100;

$total = ($intTotalPrice + $intAddend + $intTaxRate) / 1000; // add all the numbers, then divide by the same number we multiplied by to get back to a float

$displayTotal = '$'.number_format ($total, 2);
var_dump($displayTotal);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement