Я разрабатываю проект корзины покупок с версией laravel 6.1.0. Я хочу отобразить общее количество выбранных пользователем элементов, отобразить его список корзины. Проблема в том, что я смог добавить элемент в список, но на нем не отображается добавленный список элементов.
Вот мой файл Cart.php
<?php
namespace App;
class Cart
{
public $items = null;
public $totalQty = 0;
public $totalPrice =0;
public function _construct($oldCart)
{
if($oldCart)
{
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function add($item, $id)
{
$storedItem= ['qty' => 0, 'price' => $item->price,'item'=>$item];
if($this->items){
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;
}
}
Вот мой контроллер
<?php
namespace App\Http\Controllers;
use App\Product;
use Session;
use App\Cart;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function getIndex(){
$products = Product::all();
return view('shop.index',['products' => $products]);
}
public function getAddToCart(Request $request, $id)
{
$product = Product::find($id);
$cart = Session::has('cart') ? Session::get('cart') : null;
if(!$cart)
{
$cart = new Cart($cart);
}
$cart->add($product, $product->id);
$request->session()->put('cart',$cart);
// dd($request->session()->get('cart'));
return redirect()->route('product.index');
}
public function getCart()
{
if(!Session :: has('cart') ){
return view ('shop.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view ('shop.shopping-cart' , ['products' => $cart->items, 'totalPrice'=>$cart->totalPrice]);
}
}
Вот Shopping-cart.blade.php
@extends('layouts.master')
@section('title')
Laravel Shopping Cart
@endsection
@section('content')
@if(Session::has('cart'))
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<ul class="list-group">
@if(!empty($products) && count($products) > 0)
@foreach($products as $product)
<li class ="list-group-item">
<span class ="badge"> {{ $product['qty'] }}</span>
<strong >{{ $product['item']['title']}}</strong>
<span class ="label label-success"> {{ $product['price']}}</span>
<div class="btn-group">
<button type="button" class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">Action<span class="caret"></span></button>
<ul class="dropdown-menu">
<li> <a href="#"> Reduce By 1 </a> </li>
<li> <a href="#"> Reduce All </a> </li>
</ul>
</div>
</li>
@endforeach
@endif
</ul>
</div>
</div>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<strong > Total : {{ $totalPrice}}</strong>
</div>
</div>
<hr>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<button type="button" class="btn btn-success">Checkout</button>
</div>
</div>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<strong>Total : {{$totalPrice}} </strong>
</div>
</div>
<hr>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<button type="button" class ="btn-btn-success">Checkout</button>
</div>
</div>
@else
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<h2>No Items In Cart</h2>
</div>
</div>
@endif
@endsection
Вот снимок экрана. ![enter image description here](https://i.stack.imgur.com/AUzXs.png)