Laravel array Session overwriting

I'm trying to write the product data to a session in Laravel, but it overwrites the session. I have an array of products in 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);
}
Author: sant0will, 2018-11-07

1 answers

You do not need to test whether the cart Key already exists in the session. The method Session::push() will add an item to an array in the session, if that array does not exist it creates automatically.

If you look at the source code you will see this code:

/**
 * Push a value onto a session array.
 *
 * @param  string  $key
 * @param  mixed   $value
 * @return void
 */
public function push($key, $value)
{
    $array = $this->get($key, []);
    $array[] = $value;
    $this->put($key, $array);
}

The line:

$array = $this->get($key, []);

Ensures that if the key does not exist, an array is created by default.

Then your code would be:

protected function addCart(Request $request, $id)
{
    Session::push('cart', $id);
    $cart = Session::get('cart'); 
    return redirect('/product/category/3/carnes')->with('cart', $cart);
}
 1
Author: fernandosavio, 2018-11-07 16:29:29