从购物车会话中删除商品。 Laravel

时间:2016-06-05 10:13:04

标签: php laravel session

当我通过会话将产品添加到购物车时,我希望能够逐个从购物车中删除它们。我该怎么做?这是我的<div>

CartController

这是我的购物车:

public function add(Request $request, $id)
{
    $product_from_db = Product::find($id);
    $product = [];
    $product['id'] = $id;
    $product['title'] = $product_from_db->title;
    $product['image'] = $product_from_db->image;
    $product['slug'] = $product_from_db->slug;
    $product['price'] = $product_from_db->price;
    $product['quantity'] = $request->input('quantity');
    $request->session()->push('cart', array_merge((array)Session::get('cart',[]), $product));
    flash()->success('Prekė sėkmingai pridėtą į krepšelį.');
    return redirect('/prekes');
}

public function show()
{
    $products = session('cart');
    $subtotal = [];
    return view('cart.show')->with(array(
        'products'    => $products,
        'subtotal' => $subtotal
    ));
}

你能举个例子说明我应该如何从购物车中删除我想要的商品吗?

我试过这样做:

                        @foreach($products as $p)
                        <tr>
                            <td class="col-md-6">

                                <div class="media">
                                    <a class="thumbnail pull-left" href="#"> <img class="media-object" src="{{URL::to($p['image'])}}" style="width: 72px; height: 72px;"> </a>
                                    <div class="media-body">
                                        <a style="padding-left: 2%" href="{{URL::to('prekes/'.$p['slug'])}}" class="text-warning"><strong>{{$p['title']}}</strong></a>
                                    </div>
                                </div>

                            </td>
                            <td class="col-md-1" style="text-align: center">
                                <input type="number" class="form-control" id="quantity" value="{{$p['quantity']}}">
                            </td>
                            <td class="col-md-1 text-center"><strong>{{number_format($p['price'], 2, '.', ',')}} €</strong></td>
                            <td class="col-md-1 text-center"><strong>{{number_format($p['price']*$p['quantity'], 2, '.', ',')}} €</strong></td>
                            <td class="col-md-1">
                                <button type="button" class="btn btn-danger">
                                    <span class="glyphicon glyphicon-remove"></span> DELETE ITEM
                                </button></td>
                        </tr>
                        @endforeach

我收到此错误:public function delete($id) { $product = session::forget('cart', $id)->first(); $product->destroy($id); return redirect()->back(); } <a href="{{URL::to('jusu-prekiu-krepselis/trinti-preke/'.$p['id'])}}" type="button" class="btn btn-danger"> <span class="glyphicon glyphicon-remove"></span> Remove </a>

好的,当我这样做时,它会从数据库中删除产品,而不是从会话中删除。

现在,当我删除时,我会收到此错误:Call to a member function first() on null

1 个答案:

答案 0 :(得分:0)

要删除$id的特定项目,请更改

public function delete($id)
{
    $product = session::forget('cart', $id)->first();
    $product->destroy($id);
    return redirect()->back();
}

public function delete($id)
    {
        $products = session('cart');
        foreach ($products as $key => $value)
        {
            if ($value['id'] == $id) 
            {                
                unset($products [$key]);            
            }
        }
        //put back in session array without deleted item
        $request->session()->push('cart',$products);
        //then you can redirect or whatever you need
        return redirect()->back();
    }

希望有所帮助:)