Laravel array Session overriding

Asked

Viewed 159 times

0

I’m trying to record product data in a 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);
}

1 answer

1


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

If you look at the source code 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.