labas GamePage visogero labas GamePage visogero - 6 months ago 107
PHP Question

Deleting item from cart session. Laravel

When I add products to my cart through session I want to be able to delete them from my cart one by one. How I'm supposed to do that ? This is my

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
));
}


This is my cart:

@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


Can you give me some example how I'm supposed to delete item that I want from a cart ?

I've tryed to do this:

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>


I get this error:
Call to a member function first() on null


Okay when I do this it removes the product from database not from the session

Answer

To delete specific item with $id change your

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

with

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();
    }

Hope it helps :)

Comments