I am trying to multiply the total dollar amount of cart items by a sales tax amount and have that displayed in my view. It seems to be working, or at least I get no errors, but I get no tax displayed in my view.
In my payment controller I have this
public function showPayment() { $cart = Session::get('cart'); $payment_info = Session::get('payment_info'); if($payment_info['status'] == 'on_hold') { $sales_tax = $cart->totalPrice * .085; return view('cart.payments', ['payment_info' => $payment_info], ['cartItems' => $cart]); $sales_tax = $cartItems->totalPrice * .085; }else{ return redirect()->route("home"); } }
And in my view I have this
<li class="payment__item">Taxes: <span>{{ $sales_tax }} </span> </li>
All my other data is working properly, just not the sales tax.
Advertisement
Answer
What @Bahaedin was implying is that you need to specifically define the sales tax variable if you want to use it as it is in the view. And, since it’s bad practice to do logic/calculations in blade, here is one approach that would solve your issue.
if($payment_info['status'] == 'on_hold') { $sales_tax = $cart->totalPrice * .085; //move this before the return statement && cart seems to be an object. return view('cart.payments', ['payment_info' => $payment_info, 'cartItems' => $cart, 'sales_tax' => $sales_tax]); //one array, include $sales_tax as a variable.
And in your blade
<li class="payment__item">Taxes: <span>{{ $sales_tax }} </span> </li>
Also, note that if you are sending $cartItem
variable into the view, then you would need to reference it using $cartItem
not $cart_item
so $cart_item['data']['sales_tax']
would not have worked even if you had sent sales_tax in a $cartItem
array. If the way you did the multiplication is any hint, I would say $cart is an object, not an array.