问题
I'm from the same post here but I've found the root problem why is my quantity won't increment but I don't know how to solve this. The problem is at my isset function (in Cart.php), because I tried to echo something there and just realized the function isn't running. This is the error pops up when I removed isset function at the if($this->items). I tried to dd($items) too, and it said null. Why is my $items isn't holding a group of $item? Do you guys know why and how to solve this?
p/s: from the previous post, I've removed my $oldCart and replace it with $cart, and I'm expecting $cart to overwrite itself. These are all the codes related to my shopping cart.
In Cart.php
<?php
namespace App\Models;
class Cart
{
public $items = array();
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($cart)
{
if ($cart) {
//dd($this);
$this->items = $cart->items;
$this->totalQty = $cart->totalQty;
$this->totalPrice = $cart->totalPrice;
}
}
public function add($item, $id)
{
global $cart;
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 ($this->$items) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
//dd($items);
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
}
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;
class FoodController extends Controller
{
public function addtoCart(Request $request, $id, Food $foods)
{
$foods = Food::find($id);
//check in session if cart already contains product
$cart = Session::has('cart') ? Session::get('cart') : null;
//dd($cart);
//if it did contain products, pass them to constructor
if (!$cart) {
$cart = new Cart($cart);
}
$food = $foods->id;
$cart->add($foods, $food);
Session::put('cart', $cart);
//dd($request->session()->get('cart'));
return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
public function getCart()
{
if (!Session::has('cart')) {
return view('cart');
}
$cart = Session::get('cart');
$cart = new Cart($cart);
return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
}
in cart.blade.php
@foreach ($food as $foods)
<p align="left"><a href="#"> {{ $foods['item']['name'] }} </a> <span class="price">MYR {{ $foods['price'] }} </span></p>
<p align="left">Quantity</p>
<p><input class="w3-input w3-p-adding-16 w3-border" type="number" placeholder="Quantity" value="{{ $foods['qty'] }}" required name="quantity"></p>
@endforeach
<hr>
<p align="left">Total <span class="price"><b>MYR {{ $totalPrice }}</b></span></p>
回答1:
I think you need to re-define your add function in the Cart class to be like this, there are some syntax errors, (remove $
sign form $this->$items
), and there is no need to re-declare $cart & $items in the function
public function add($item, $id)
{
//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) ? $this->items : false) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
//dd($items);
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
回答2:
The error you get is because of the way you try to get a property from $this cart object.
Change in line 39 (inside the add method):
if (isset($this->$items) ? $items : false)
To:
$this->items
Because in the way you currently have it defined, it tries to access a property on $this that has the value of the variable $items, inside your conditional.
This is also the reason why the error points to Cart::$ since items as a variable holds no value at that point in time.
来源:https://stackoverflow.com/questions/65491706/why-is-my-items-doesnt-hold-a-group-of-item-laravel-8-ecommerce