Laravelshoppingcart: Cart::content() is empty when redirecting

Created on 18 Mar 2017  Â·  18Comments  Â·  Source: Crinsane/LaravelShoppingcart

~~~~php
class CartController extends Controller
{

public function add(Request $request)
{
    //Cart::add('192ao12', 'Product 1', 1, 9.99);
    //Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);
    //Cart::destroy();
    //
    $id             = strtotime("now");
    $product        = Product::find($request->product_id); 
    $product_name   = $request->product_name;
    Cart::add($id, $product_name, 1, $product->price);
    return redirect('/cart');
    //dump(Cart::content());
}

public function cart()
{
    return view('FrontEnd::cart.index');
}

}
~~~~
when i dump in add() cart->content is showing,
but when redirecting there are empty,

how can i store it?

sory for my englist

Most helpful comment

@jonathan-bird Did you call Cart::content() together with the dd() helper? Because that will stop Laravel from writing the session.

All 18 comments

Hi. It seems like you have an issue with your session. Which Laravel version are you using?

Hi, it is the same for me with Laravel 5.4 since today.

Here is the require section of my composer.json

    "require": {

        "php": ">=5.6.4",
        "anhskohbo/no-captcha": "^2.3",
        "barryvdh/laravel-translation-manager": "^0.2.8",
        "brexis/laravel-workflow": "^1.0",
        "cviebrock/eloquent-sluggable": "^4.2",
        "doctrine/dbal": "^2.5",
        "gloudemans/shoppingcart": "^2.3",
        "intervention/image": "^2.3",
        "intervention/imagecache": "^2.3",
        "kris/laravel-form-builder": "1.11.*",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "maatwebsite/excel": "~2.1.0",
        "mcamara/laravel-localization": "1.2.*",
        "netshell/paypal": "dev-master",
        "spatie/laravel-translatable": "^1.2"
    },

Thanks

I've received the same in Laravel 5.4. I noticed it's only when I call Cart::content() straight after adding. When I don't do this, it seems to work. It's strange.

Make sure you have your routes in the web middleware, otherwise won't store in session. Not sure why mine was happening, it would work sometimes, but most of the time not until I removed calling content method straight away.

Any ideas?

@jonathan-bird Did you call Cart::content() together with the dd() helper? Because that will stop Laravel from writing the session.

Sure did. After the cart add had run, I ran dd of Cart content. What's the
best way to debug that?

Even when I didn't dd, it had the same issue. I have two separate api
calls, it adds to cart, then I tried to open the cart slidedown I built,
which calls cart content but 75% of the time didn't have the new item, then
I'd keep trying then viola.

After disabling the cart drop down on add to cart click, I clicked it
manually and it started working fine again.

I know this is hard to follow. I've tried to replicate it as much as
possible down to that I couldn't call it straight away.

On 3 Apr. 2017 11:23 pm, "Rob Gloudemans" notifications@github.com wrote:

@jonathan-bird https://github.com/jonathan-bird Did you call
Cart::content() together with the dd() helper? Because that will stop
Laravel from writing the session.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Crinsane/LaravelShoppingcart/issues/301#issuecomment-291141256,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABcKcQAgbhkp2-knpsASH9gMpz3BC_L0ks5rsPLdgaJpZM4MhYER
.

Hi all,
I don't think it's directly a Cart::content() problem.

Here is my code :

    public function addItem(Request $request) {

        Cart::add([
            'id'    => $request->input('id'),
            'name'  => $request->input('name'),
            'qty'   => $request->input('qty'),
            'price' => $request->input('price')
        ]);


        // return redirect()->route('shopping.cart');
        return $this->shoppingCart();
    }

The shopping.cart route is mapped to the shoppingCart() method.
With the redirection to the route, the Cart::content() is empty.
With the direct call of shoppingCart() method, Cart items are listed in Cart::content().

Hope it helps

I found that the problem was a side effect from an extended Laravel service.
I no longer have issue with LaravelShoppingCart.

Ah awesome, good find. So just update the library you mean?

On 12 Apr. 2017 7:34 pm, "jefdigital" notifications@github.com wrote:

I found that the problem was a side effect from an extended Laravel
service.
I no longer have issue with LaravelShoppingCart.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Crinsane/LaravelShoppingcart/issues/301#issuecomment-293524807,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABcKcYlWGa8dhmZ2reLLIJJxJ6rqqBqjks5rvJqWgaJpZM4MhYER
.

Not exactly,
I replaced the concerned library to an equivalent and everything works perfectly now.

The buggy library was also removing errors from form validation.... so i had to found a solution quickly :)

Hi.
I also have an issue with retrieving the cart contents. It returns empty.
This is my controller: https://pastebin.com/q0yUbcrY

I am sorry. I was doing it wrong. Everything seems fine now.

A quick fix for 5.4 would be using `return redirect()->action('YourController@method');

I have it setup like this->

My YourController has $cart = Cart::content(); and my view is getting returned with $cart.

Hi,Excuse me sir,Could you help me somthing?I would like to show the message to the user who didn't buy yet anythings in my product items.For example,"Your cart is empty".I'd like to show like that.So how can i do it?Will i check using with"If statement..if()" in my view blade?Could you answer me?I'm using Crinsane laravel shopping cart.So please,Help me.

@minaung7 Either use if (Cart::count() == 0) or if (Cart::content()->isEmpty())

Yes sir,I did it successfully.Thanks for telling me how to do it.Now I'm OK.Thank you so much,sir.

Hi @cwprogger
I have the same error, when redirecting the user after login to the checkout page the cart content is empty , but once i refresh the page it works fine.

I have tried all the above and still have the same issue

same error in version 8

@jonathan-bird Did you call Cart::content() together with the dd() helper? Because that will stop Laravel from writing the session.

when payment is successful then the API redirects to my website. After redirecting the cart::content() is empty. Why?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nasirkhan picture nasirkhan  Â·  8Comments

sobhanattar picture sobhanattar  Â·  4Comments

ChrisThompsonTLDR picture ChrisThompsonTLDR  Â·  5Comments

lagask picture lagask  Â·  8Comments

wasid picture wasid  Â·  8Comments