我的功能是:
static private function productLastViewed($request, $item){
$product_viewed = session()->get('product_viewed');
if (!$product_viewed){
$product_viewed= [$item]; //grant item first to array
session()->put('product_viewed', $product_viewed); //session for array
}else{
session()->push('product_viewed', $item); //push some item view to session array
}
return session()->get('product_viewed'); //return array in session
}
该值始终只有1个项目
请为我说出什么问题了?
答案 0 :(得分:1)
您好,您使用的是变量而不是数组。您可以添加类似的内容
static private function productLastViewed($request, $item)
{
$product_viewed = session()->get('product_viewed');
if (!$product_viewed)
{
$product_viewed[]= $item; //grant item first to array
session()->put('product_viewed', $product_viewed); //session for array
}
else
{
$product_viewed[]= $item;
session()->push('product_viewed', $product_viewed); //push some item view to session array
}
return session()->get('product_viewed'); //return array in session
}