File: /var/www/vaspayment.com/app/Http/Controllers/CartController.php
<?php
namespace App\Http\Controllers;
use App\Models\Cart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$productId = $request->input('shop_id');
$quantity = $request->input('quantity');
// $user_id = $request->input('user_id');
// Fetch the current cart items from the session
$cart = Session::get('cart', []);
// Check if the product is already in the cart
if (array_key_exists($productId, $cart)) {
// If product exists in cart, update the quantity
$cart[$productId] += $quantity;
} else {
// If product is not in cart, add it
$cart[$productId] = $quantity;
}
// Store the updated cart back into the session
Session::put('cart', $cart);
return redirect()->route('cart.index')->with('status', 'Product added to cart successfully!');
}
/**
* Display the specified resource.
*/
public function show(Cart $cart)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Cart $cart)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Cart $cart)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Cart $cart)
{
//
}
}