I'm a beginner in laravel and I've been trying to create a simple ecommerce web but i got stuck in this logic error for days. I've been following in a youtube tutorial about creating shopping cart but the item quantity won't increment if i add the same item in the cart. The totalPrice and totalQty seems counting fine though...
this is what i get when i tried to dd($this) into cart.php.
I think it's because i declared the array in the add function(in Cart.php) and it keeps overwriting the current quantity. but i cant declare it outside of the function or it'll throws an error saying undefined index. I've tried checking some of relevant posts here but it still didn't work. I don't know what to do anymore.
Do you guys know what's the solution? Thank you!
p/s: this is the video tutorial link that i watched. all creds to Academind for an amazing tutorial.
https://www.youtube.com/watch?v=4J939dDUH4M&list=PL55RiY5tL51qUXDyBqx0mKVOhLNFwwxvH&index=9
these are my related codes to the error.
In Cart.php
<?php
namespace App\Models;
class Cart
{
public $items = array();
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart)
{
if ($oldCart) {
//dd($this);
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function add($item, $id)
{
//force get $items
global $items;
//default values if item not in cart
$storedItem = [
'qty' => 0,
'price' => $item->price,
'item' => $item
];
//if item is already in shopping cart
if (isset($this->$items) ?? $items::false) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
dd($this);
}
}
In FoodController.php
<?php
namespace App\Http\Controllers;
use App\Models\Cart;
use App\Models\Food;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
public function addtoCart(Request $request, $id, Food $foods)
{
$foods = Food::find($id);
//check in session if cart already contains product
$oldCart = Session::has('cart') ? Session::get('cart') : null;
//if it did contain products, pass them to constructor
$cart = new Cart($oldCart);
$cart->add($foods, $foods->id);
$request->session()->put('cart', $cart);
return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
public function getCart()
{
if (!Session::has('cart')) {
return view('cart', ['food' => null]);
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart', ['food' => $cart->items,'totalPrice' =>$cart->totalPrice]);
}
in cart.blade.php
@foreach ($food as $foods)
<p align="left"><a href="#"> </a> <span class = "price"> $ </span></p>
<p align="left">Quantity</p>
<p><input class="w3-input w3-p-adding-16 w3-border" type="number" placeholder="Quantity" value="" required name="quantity"></p>
@endforeach
<hr class="new1">
<p align="left">Total <span class="price"><b> $ </b></span></p>
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2M4vLtq
via IFTTT
Aucun commentaire:
Enregistrer un commentaire