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
{
private $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 (isset($this->$items) ? $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;
}
}
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="#"> </a> <span class="price">MYR </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>MYR </b></span></p>
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3nXuUZv
via IFTTT
instagram takipçi satın al
RépondreSupprimerinstagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
MMORPG OYUNLAR
RépondreSupprimerİnstagram Takipci Satın Al
tiktok jeton hilesi
Tiktok jeton hilesi
antalya saç ekimi
Takipci
instagram takipçi satın al
MT2 PVP
İnstagram Takipci
YENİ PERDE MODELLERİ
RépondreSupprimernumara onay
mobil ödeme bozdurma
nftnasilalinir
Ankara evden eve nakliyat
Trafik Sigortası
DEDEKTOR
web sitesi kurma
aşk kitapları
écrire un mémoire
RépondreSupprimerGood content. You write beautiful things.
RépondreSupprimerhacklink
korsan taksi
vbet
sportsbet
mrbahis
vbet
mrbahis
taksi
hacklink
Good text Write good content success. Thank you
RépondreSupprimerbetpark
kibris bahis siteleri
slot siteleri
poker siteleri
kralbet
betmatik
bonus veren siteler
mobil ödeme bahis
burdur
RépondreSupprimeredirne
erzurum
ordu
kilis
3E0
antep
RépondreSupprimerhatay
kocaeli
tokat
uşak
M4XJJ
Laissez-vous inspirer par les agences artistiques pour créer des événements artistiques uniques. Funly (https://www.funly.fr) vous accompagne avec des artistes talentueux et des performances captivantes.
RépondreSupprimeryurtdışı kargo
RépondreSupprimerresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
QY51XZ
hatay
RépondreSupprimertunceli
amasya
manisa
ağrı
05Z5W
salt likit
RépondreSupprimersalt likit
7FG
https://saglamproxy.com
RépondreSupprimermetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
F6Y33
Quick delivery when I decided to Buy instagram followers . Support team was helpful throughout the process.
RépondreSupprimerAİYCK1
Quick delivery when I decided to Buy instagram followers . Support team was helpful throughout the process.
RépondreSupprimer8JK
Web sitesi kurma konusunda verdikleri hizmet muhtesem. 2 gunde sitem hazirdi.
RépondreSupprimer8LQZ0
شركة تسليك مجاري بالاحساء WRwjIIzplr
RépondreSupprimerشركة تسليك مجاري بالدمام WE5Nu2j5Lp
RépondreSupprimer