简体   繁体   English

所有用户的购物车都相同Laravel

[英]Shopping cart is the same for all users Laravel

I am working on a project with laravel, and I want every user to have his own cart which means if user one adds item to cart, deletes or update it should only show in his account without affecting other users. 我正在与laravel合作开发一个项目,我希望每个用户都拥有自己的购物车,这意味着如果用户将商品添加到购物车,删除或更新该商品,则该商品只能显示在其帐户中而不影响其他用户。 For now when a user adds item or deletes it shows in all users. 目前,当用户添加或删除项目时,它会在所有用户中显示。

I have tried some solutions from other source but didn't work. 我尝试了其他来源的一些解决方案,但是没有用。

ProductController.php ProductController.php

public function cart()
{
    return view('cart');
}

public function addToCart($id)
{
    $userId = Auth::user()->id;
    $products = products_model::where('user_id', $userId)->find($id); 

    if(!$products) {
        abort(404);
    }

    $cart = session()->get('cart');

    // if cart is empty then this the first product
    if(!$cart) {

        $cart = [
            $id => [
                "pro_name" => $products->pro_name,
                "quantity" => 1,
                "pro_price" => $products->pro_price,
                "image" => $products->image
            ]
        ];

        session()->put('cart', $cart);

        return redirect()->back()
                    ->with('success', 'Product added to cart successfully!');
    }

    // if cart not empty then check if this product exist then increment quantity
    if(isset($cart[$id])) {

        $cart[$id]['quantity']++;

        session()->put('cart', $cart);

        return redirect()->back()
                    ->with('success', 'Product added to cart successfully!');

    }

    // if item not exist in cart then add to cart with quantity = 1
    $cart[$id] = [
        "pro_name" => $products->pro_name,
        "quantity" => 1,
        "pro_price" => $products->pro_price,
        "image" => $products->image
    ];

    session()->put('cart', $cart);

    return redirect()->back()
                ->with('success', 'Product added to cart successfully!');
}

public function update(Request $request)
{
    if($request->id and $request->quantity)
    {
        $cart = session()->get('cart');

        $cart[$request->id]["quantity"] = $request->quantity;

        session()->put('cart', $cart);

        session()->flash('success', 'Cart updated successfully');
    }
}

public function remove(Request $request)
{
    if($request->id) {

        $cart = session()->get('cart');

        if(isset($cart[$request->id])) {

            unset($cart[$request->id]);

            session()->put('cart', $cart);
        }

        session()->flash('success', 'Product removed successfully');
    }
}

I tried to add these lines (add to cart) but isn't working. 我试图添加这些行(添加到购物车),但无法正常工作。

$userId = Auth::user()->id;
$products = products_model::where('user_id', $userId)- >find($id);

Yeah you can do. 是的,你可以。 Its called Replication . 其称为复制

Just add one more field like userID in Products table and save users id on it. 只需在“产品”表中再添加一个字段,例如userID,然后在其中保存用户ID。

To shown products to all users simply put top id(first id, probably system admin id) 要向所有用户显示产品,只需放置顶部ID(第一个ID,可能是系统管理员ID)

So everytime when you want show, edit, update or delete you simply do with product id as well as user id. 因此,每次您要显示,编辑,更新或删除时,只需使用产品ID和用户ID。

Hope this will works. 希望这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM