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

david001's avatar

How to add items in Session Vue js and Laravel

I want to store items in cart with laravel session and sending selected items from vue js to laravel and display them back in the page. Here i want to display the lists, but i got nothing. I want get the session items

{{ product.name }}

{{ lists }}

</p>

<button class="btn btn-primary" @click="addProductToCart(product)">Add to cart</button>
</div>

<script type="text/javascript">
    export default{
        props:['product'],
        data(){
            return{
                lists:[],

            }
        },
        mounted(){
                        
            axios.get('/list')
            .then((response)=>{
            this.lists= response.data
            });
        },
        methods:{
            addProductToCart(product){
                
                axios.get('/store-in-cart',{
                    
                    product : product
                }).then((response)=>{
                console.log(response)
            });;
            
            }
        }
    }
</script>;

controller

public function StoreInCart(Request $request){
       $products = \Session::put('product',$request->product);
        return $products;
    }
    public function getProduct(Request $request){
        $product = \Session::get('product');
         return $product;
    }
0 likes
7 replies
ahmeddabak's avatar

Do you have any errors, or just wanna know the best way to add items in the session using vuejs

signar's avatar

You could try this:

public function StoreInCart(Request $request) {
    session(['product' => $request->product]);

    return session('product');
}

If that doesn't work, make sure that you're supplying Vue with the CSRF-token and that you are hitting the right endpoint.

Please open the developer tools in your browser and paste the error.

david001's avatar

@SIGNAR - I tried

    public function StoreInCart(Request $request){
      
         session(['product' => $request->product]);

         return session('product');
    }

    public function getProduct(Request $request){
//I visited this route- returns only one items though i addded many items in cart
        $product = \Session::get('product');
         return $product;
    }

After i clicked the add to cart button for many items and i visited /list route i got only one item but it should show all items stored in Session, not only one.

signar's avatar
signar
Best Answer
Level 27

@david001 @andreaschrist27 is right. Session push is the correct solution. Try this:

public function StoreInCart(Request $request) {
    session()->push('products', $request->product);

    return session('products');
}

Please or to participate in this conversation.