laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

Item not remove

Open SknightS opened this issue 7 years ago • 7 comments

$cartid = $r->cartid; // return $cartid; Cart::remove($cartid);

i am trying like this . but item not remove from cart . what can be the issue ?

SknightS avatar Mar 27 '18 19:03 SknightS

@SknightS $cartid is the ID of what, of the item (product) you want to delete? what result does dd($cartid); show? Is it the ID of the item you want to be removed?

frezno avatar Mar 27 '18 21:03 frezno

yes its the item id . its showing nothing .

SknightS avatar Mar 28 '18 05:03 SknightS

if dd doesn't give you a result, it means that the item id isn't provided, ie there's something wrong with your code.

for displaying your items in the cart, you use $cartCollection = Cart::getContent(); and pass the result to your view and loop through it, eg

@foreach ($cartCollection as $cart)
    <tr>
        <td>{{ $cart['name'] }}<td>
        <td>{{ $cart['qty'] }}<td>
        <td>{{ $cart['price'] }}<td>
        <td><a class="btn btn-danger" href="{{ url('/remove/'.$cart['id']) }}">X</a><td>
    </tr>
@endforeach

in your web.php file you've to add the route: Route::get('remove/{id}', 'CartController@removeFromCart');

and the removeFromCart method:

public function removeFromCart($id)
{
    Cart::remove($id);

    return back();
}

that way it shouldn't be a problem to delete an item from the cart

frezno avatar Mar 28 '18 07:03 frezno

thanks for your help .

but i did it with ajax . i pass the id with ajax and in controller i remove the item . yes i show that item is in the cart .

is it the problem of ajax controller ?

SknightS avatar Mar 28 '18 07:03 SknightS

sorry, don't know how to handle ajax

frezno avatar Mar 28 '18 11:03 frezno

@SknightS please post your codes or you can always check the demo repo here https://github.com/darryldecode/laravelshoppingcart-demo this is ajax.

darryldecode avatar Mar 28 '18 14:03 darryldecode

if dd doesn't give you a result, it means that the item id isn't provided, ie there's something wrong with your code.

for displaying your items in the cart, you use $cartCollection = Cart::getContent(); and pass the result to your view and loop through it, eg

@foreach ($cartCollection as $cart)
    <tr>
        <td>{{ $cart['name'] }}<td>
        <td>{{ $cart['qty'] }}<td>
        <td>{{ $cart['price'] }}<td>
        <td><a class="btn btn-danger" href="{{ url('/remove/'.$cart['id']) }}">X</a><td>
    </tr>
@endforeach

in your web.php file you've to add the route: Route::get('remove/{id}', 'CartController@removeFromCart');

and the removeFromCart method:

public function removeFromCart($id)
{
    Cart::remove($id);

    return back();
}

that way it shouldn't be a problem to delete an item from the cart

ThankssSSS!!!!!Bro - you best!

Generallisimo avatar Jan 14 '23 07:01 Generallisimo