On my laravel shopping cart i create a delete function, and seems to be ok when testing with json or dd function, but where is an issue when trying to return a route cart view Route [cart] not defined.
. The function also needed to be optimized when i want to delete only 1 item of the same product, which means that is needed a "Delete 1 item" and "Delete all items" per row.
Cart model:
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, 'imagePath' => $item->imagePath];
if ($this->items ) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$storedItem['imagePath'] = $item->imagePath;
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
public function delete($id) {
if(!$this->items || !isset($this->items[$id])) {
return false; // maybe throw an exception here?
}
data of $this->items, create a method to calculate the totals when needed.
$this->totalQty -= $this->items[$id]['qty'];
$this->totalPrice -= $this->items[$id]['price'] * $this->items[$id]['qty'];
// and remove the item
unset($this->items[$id]);
}
}
Cart controller:
class ProductController extends Controller
{
/**
*@return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return view('home', ['products'=> $products]);
}
public function getAddToCart(Request $request, $id)
{
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->route('home');
}
public function getCart()
{
if (!Session::has('cart')) {
return view('cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return \Illuminate\Http\Request
*/
public function destroy(Request $request, $id) {
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->delete($id);
$request->session()->put('cart', $cart);
return redirect()->route('cart');
//return response()->json(array( 'totalqty' => $cart->totalQty, 'totalPrice' => $cart->totalPrice));
//{"totalqty":0,"totalPrice":-222000}
}
}
Cart view:
<form method="POST" action="{{ route('cart.destroy', ['id' => $product['item']['id']]) }}" >
@csrf
{{ method_field('DELETE') }}
<input type="submit" value="Delete all items" onclick="return confirm('Are you sure?')" class="btn btn-danger" />
<input type="submit" value="Delete 1 item" onclick="return confirm('Are you sure?')" class="btn btn-danger" />
</form>
I solved the issue with route cart from return redirect()->route('cart');
to return redirect()->route('shoppingCart');
.