1 year ago

how can i make a data available to all views

Posted 1 year ago by Lestah

i have a cart button whick displays how many cart items on the cart like this cart ( 0 ) what i did is

here's my controller

public function cart()
    $session_id = Session::get('session_id');
    $countCartItems = DB::table('cart')->where(['session_id'=>$session_id])->count();
    return view('products.cart')->with(compact('countCartItems'));

on my view cart.blade.php i can see that it's woking it display's how many items are there in cart with the same session_id now my problem is when i go to other pages like the homepage i get this error Undefined variable: countCartItems

how can i make this {{ $countCartItems }} variable to be available in all pages

Please sign in or create an account to participate in this conversation.