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?
$products = session('cart');
I try with
$products = session('cart');
how to check then if exist in cart
$product = session('cart', $product);
// or
$product = session()->get('cart', $product);
don't know off hand whether first example works the way you expect
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();
}
but I need little help how to push message if item is in cart
Please or to participate in this conversation.