Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Raimondas's avatar

Add new data to session from view input

How can I add new data into my session ? I'm creating a cart app for now I can add items and view them in my cart but I don't know how to add quantity value for every item and store it in a session. This is my CartController

    public function add(Request $request, $id)
    {
        $request->session()->push('cart', $id);
        flash()->success('Prekė sėkmingai pridėtą į krepšelį.');
        return redirect('/prekes');
    }
    
    public function show()
    {
        $products = session('cart');
        $products = Product::find($products);
        return view('cart.show', compact('products'));
    }

This is product view page:

        <div class="mainContainer">
            <h1>{{$product->title}} <i class="fa fa-shopping-cart" aria-hidden="true"></i></h1><br>
                    <img style="height: 200px" src="{{$product->image}}"><br>
                    <p>{!! $product->description !!}</p>
            <div class="bootstrap-wrapper">
                <a style="float: right; margin-top: -150px; margin-right: 300px; font-size: 24px"
                   href="/prideti-i-krepseli/{{$product->id}}" class="btn btn-success">
                    <i class="glyphicon glyphicon-shopping-cart"></i> Pridėti į krepšelį</a><br>
                {{number_format($product->price, 2, '.', ',')}} EUR
            </div>

How to add something like this in the product page:

<input name="quantity" type="number">

and then assign it into session ? Add controller method

0 likes
0 replies

Please or to participate in this conversation.