I'm trying to write product data to a session in Laravel
, but it overwrites the session. I have an array of products in the view:
@foreach($products as $product)
<form action="{{ url('/cart/add') }}/{{ $product->id }}" method="post">
@csrf
{!! Form::hidden('id', $product->id) !!}
{!! Form::hidden('name', $product->name) !!}
{!! Form::hidden('description', $product->description) !!}
{!! Form::hidden('price', $product->price) !!}
<div class="rowcolunlist w-row">
<div class="column-3 w-col w-col-2">
<div class="divfotoprodlist"><img src="{{ url('storage/products/'. $product->image) }}" width="174" class="fotoprodlist"></div>
</div>
<div class="column-4 w-col w-col-6">
<div class="divnomeprodlist">
<div class="nomeprodsales list">{{ $product->name }}<br></div>
<div class="nomeprodsales list description">{{ $product->description }}<br></div>
<div class="nomeprodsales list codigo">#{{ $product->sku }}<br></div>
</div>
</div>
<div class="column-5 w-col w-col-2">
<div class="divprecoprodlist">
<div class="priceprodsales list">$ {{ $product->price }}<br></div>
<div class="priceprodsales list kg">(por kilo)<br></div>
</div>
</div>
<div class="column-6 w-col w-col-2">
<div class="divbotaoprodlist">
<button type="submit" title="{{ $product->id }}" class="btaddcartsales list w-button">Add Cart</button>
</div>
</div>
</div>
</form>
@endforeach
Controller:
protected function addCart(Request $request, $id)
{
if(Session::has("cart")){
Session::push('cart', [$id]);
} else {
Session::put('cart',[$id]);
}
$cart = Session::get('cart');
return redirect('/product/category/3/carnes')->with('cart', $cart);
}