Laravel array session overwriting

0

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);
}
    
asked by anonymous 07.11.2018 / 17:01

1 answer

1

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

If you look at 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 look like:

protected function addCart(Request $request, $id)
{
    Session::push('cart', $id);
    $cart = Session::get('cart'); 
    return redirect('/product/category/3/carnes')->with('cart', $cart);
}
    
07.11.2018 / 17:29