bumbummen99 / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
507 stars 235 forks source link

Could not get the Cart::content() from session :( #123

Closed hardkiffeur closed 3 years ago

hardkiffeur commented 3 years ago

Hi every one, Laravel 8.12 / LaravelShoppingcart 4.0

My CartController :

...
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Product;
use Cart;
...
    public function add(Request $request) {

        $product = Product::findOrFail($request->id);

        Cart::instance('shopping')
            ->add([
            'id' => $product->id,
            'name' => $product->name,
            'price' => $product->price,
            'weight' => $product->weight,
            'qty' => $request->quantity,
            ])
            ->associate('Product');
           //dd(Cart::instance('shopping')->content());

        /* Redirect to prevend re-adding on refreshing */

        return redirect()->back()->with('cart', 'Product has been successfully added to the Cart.');
    }
...

The dd(Cart::instance('shopping')->content()); output :

Illuminate\Support\Collection {#1443 ▼
  #items: array:1 [▼
    "8a48aa7c8e5202841ddaf767bb4d10da" => Gloudemans\Shoppingcart\CartItem {#1448 ▼
      +rowId: "8a48aa7c8e5202841ddaf767bb4d10da"
      +id: 6
      +qty: "1"
      +name: "Exercitationem et eum vel."
      +price: 47.5
      +weight: 2.22
      +options: Gloudemans\Shoppingcart\CartItemOptions {#1442 ▼
        #items: []
      }
      +taxRate: 21
      -associatedModel: "Product"
      -discountRate: 0
    }
  ]
}

My Product.blade.php show me the 'cart' flah message and I could open my #cartModal after the addCart action but always giving me an Empty cart

 <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#cartModal">
        Launch demo modal
    </button>

    @if(session('cart'))
    <!-- Modal -->
    <div class="modal fade" id="cartModal" tabindex="-1" aria-labelledby="cartModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="cartModalLabel">Cart in this session {{ $cartCount ?? ''}}</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    @if (count(Cart::instance('shopping')->content()))
                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">Name</th>
                                <th scope="col">Price</th>
                                <th scope="col">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (Cart::content() as $item)
                            <tr>
                                <td>{{ $item->name }}</td>
                                <td>{{ $item->model->price }}</td>
                                <td>{{ $item->total }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                    @else
                    <div class="m-0 text-center alert alert-info" role="alert">
                        Your Cart is <b>empty</b>.
                    </div>
                    @endif
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
@endif
....

With this addCart form :

 <form id="buyForm" method="POST" action="{{ route('add-product', ['id' => $product->id])}}">
                @csrf
                <div class="row">
                    ...
                </div>
                <div class="mb-4 input-group w-100">
                    <input class="input-invisible form-control form-control-lg detail-quantity" name="quantity" type="number" value="1">
                    <input class="input-invisible form-control form-control-lg detail-quantity" name="id" type="number" value="{{$product->id}}">
                    <input class="input-invisible form-control form-control-lg detail-quantity" name="price" type="number" value="{{$product->price}}">
                    <input class="input-invisible form-control form-control-lg detail-quantity" name="weight" type="number" value="{{$product->weight}}">
                    <div class="input-group-append flex-grow-1">
                        <button class="btn btn-dark btn-block" type="submit" id="addcart"> <i
                                class="mr-2 fa fa-shopping-cart"></i>Add to Cart</button>
                    </div>
                </div>
                ...
            </form>

My routes :

Route::name('cart')->get('/cart', [FrontCartController::class, 'index']);
/* Add Product routes */
Route::name('add-product')->post('add-product/{id}', [FrontCartController::class, 'add']);

No error, and when I'm looking on /storage/framework/sessions I don't have any 'cart' trace at this time, so i'm thinking about a session issue but I really don't know where to search, any help ?

No error on Nginx No error on broswer devtools :(

hardkiffeur commented 3 years ago

Seems it's ok if I flash the redirect with an other 'name' in my CartController like this

return redirect()->back()->with('AddTocart', 'Product has been successfully added to the Cart.');