GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
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)
    {
        //
    }
}