Laravel如何存储用户访问过的产品

时间:2016-07-04 08:02:20

标签: laravel laravel-5 laravel-5.2

我正在使用laravel framework 5.2开发电子商务应用程序。我的客户想要显示产品的心愿单。用户访问产品添加愿望清单。我想在cookie中完成工作,但我无法找到它在cookie中存储,销毁和修改的方式

2 个答案:

答案 0 :(得分:-1)

阅读Laravel网站上的cookie documentation

直接引用Laravel文档:

// read a cookie value
$value = $request->cookie('name');

// write a new cookie
$response->withCookie('name', 'value', $minutes);

// write a cookie that never expires:
$response->withCookie(cookie()->forever('name', 'value'));

答案 1 :(得分:-1)

我解决了这个问题,我做了一个中间件,我得到了产品ID,然后发现它是有效的产品ID,如果有效则存储在cookie中

public function handle($request, Closure $next)
    {
        $response = $next($request);
        if ($request->hasCookie('visited_product')) {
            $info = Cookie::get('visited_product');
            if (!in_array($request->product_id, $info)) {
                $info[] = $request->product_id;
                return $response->withCookie(cookie()->forever('visited_product', $info));
            }
            return $response;

        } else {
            $product_list = [];
            $product_list[] = $request->product_id;
            return $response->withCookie(cookie()->forever('visited_product', $product_list));
        }

    }