Search code examples
laravellaravel-5cartshopping-cart

Empty cart content when submitted


I have a really basic cart that simply sends off an email when submitted. But I can't get the items to clear out, I have an if statement on the checkout page to empty it but it does not seem to work properly.

Here is the code for that blade:

    <article class="lead-in-aside">
            @if(session()->has('success'))
                (Cart::content('empty'));
            @endif
            <h2>Check Out</h2>
            <form action="{{ route('checkout submit') }} " method="POST">
                {{ csrf_field()}}
                <fieldset>
                    <div class="table-wrapper">
                        <div class="table-inner">
                            <table class="table-content">
                                <tbody>
                                    <tr>
                                        <th>Title</th>
                                        <th>Name</th>
                                        {{-- <th>Quantity</th> --}}
                                    </tr>
                                    @foreach (Cart::content() as $item)
                                    <tr class="highlight">
                                        <td>{{$item->model->title}} </td>
                                        <input type="hidden" name="{{$item->model->title}}">
                                        <td>{{$item->model->name}}</td>
                                        <input type="hidden" name="{{$item->model->name}}">
                                        {{-- <td>{{ $item->qty }}</td> --}}
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </fieldset>

And here is the code in the controller:

        Mail::send('emails.rental', [
        'name'=>$request->firstname,
        'company'=>$request->companyname,
        'phone'=>$request->phone,
        'email'=>$request->email,
        'msg'=>$request->message,
        'rental'=>$request->name,
        'pickup'=>$request->pickup_date,
        'return'=>$request->return_date,
        'location'=>$request->location
    ], 


        function($newRental) use ($request){
        $newRental->from($request->email, $request->firstname);


    });

    // Session::flash('success', 'Your Email was Sent!');

    return redirect()->back()->with('success', 'Your rental request has been sent');
 }

Solution

  • I am guessing you are using a third-party shopping cart package, like https://github.com/Crinsane/LaravelShoppingcart?

    If this is the package you are using, from the docs it looks like it is the destroy() function you should be using to clear the cart?

    My suggestion would be to try clearing the cart in the controller, once you are done sending the email, instead of in the blade file. So remove this part of your blade file:

    @if(session()->has('success'))
        (Cart::content('empty'));
    @endif
    

    and the end of your controller might become something like:

    Cart::destroy();
    return redirect()->back()->with('success', 'Your rental request has been sent');