I have a little problem with the Session variable in Laravel. You see,I fill the Session variable cart
, in the last method, but I cannot access to the data from another method, only from the addProduct()
method, I do not know what more to do, I have already read the documentation of my version and posts of third persons and nothing works for me.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Productos;
use Session;
class ShoppingCart extends Controller
{
public function getAmount(){
$total = 0;
$cart = Session::get('cart');
if($cart == null){
foreach($cart as $product) {
$total += Productos::find($product)->precio;
}
}
return $total;
}
public function getProducts()
{
return Session::get('cart');
}
public function addProduct($id)
{
if(Session::get('cart') == null){
$cart = [$id];
Session::put('cart', $cart);
}else{
$cart = array_merge(Session::get('cart'), [$id]);
Session::put('cart', $cart);
}
var_dump(Session::get('cart'));
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire