Do you have any errors, or just wanna know the best way to add items in the session using vuejs
Nov 24, 2018
7
Level 5
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;
}
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.