mardi 25 juillet 2017

Cart session and displaying number of items in cart return undefined in Laravel

I have Cart() button at the top of the page where it's stores and shows user cart number of products. It's visible also on non-logged in users and they can place products in cart. In Laravel 4.2 this works perfectly but I've migrated to Laravel 5.4 and no I facing this problem.

This is the function

protected function setupLayout()
{
    if (!is_null($this->layout)) {
        $this->layout = View::make($this->layout);
    }
    $user = self::getCurrentUser();       
    View::share('user', $user);

     // this is the part which cause error
    View::share('cartCount', count(Session::get('cart', array())));
}

public static function isLoggedIn()
{
    $user = Auth::user();
    if ($user !== null) {
        return true;
    } else {
        return false;
    }
}

This I have in my view

@if(!Auth::check())

    <li><a href="">Cart (  )</a></li>

@else 

    <li><a href="">Cart (  )</a></li>

@endif

The error

Undefined variable: cartCount

Any ideas why this works on ver 4.2 but not work on ver 5.4 Laravel?

dd(Session::get('cart', array())) return Array () which is correct if there is no products in cart. When there are products it is showing in array.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire