Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bajro17's avatar

Laravel session check

I need help I trying to make Laravel Cart with Session and I push items in cart with

$product = Product::find($id);
Session::push('cart', $product);

how to check if item exist in cart?

0 likes
6 replies
bajro17's avatar

I try with

$products = session('cart');

how to check then if exist in cart

frezno's avatar
$product = session('cart', $product);

// or
$product = session()->get('cart', $product);

don't know off hand whether first example works the way you expect

bajro17's avatar

finally I make it :) this is my code maybe it will help someone

public function store($id)
    {
      $product = Product::find($id);
      $products = session('cart');
      $a = true;
      if($products != null) {
      foreach ($products as $pro) {
        if($product->name == $pro->name){
          $a = true;
          break;
        }
        else {
          $a = false;
        }

      }
      if($a == false)
      {
        Session::push('cart', $product);
      }
}
 else {
  Session::push('cart', $product);
}

      return back();

    }
bajro17's avatar

but I need little help how to push message if item is in cart

Please or to participate in this conversation.