简体   繁体   English

使用 Cookie 或 Session 哪个? laravel 5.7 - 存储数据 5 分钟

[英]which to use Cookie or Session? laravel 5.7 - to store data for 5 minutes

guys I'm trying to have 4 steps payment in my application in laravel 5.7伙计们,我正在尝试在 laravel 5.7 的应用程序中进行 4 步付款

then I wanna store these data in somewhere 4 steps until the payment have been completed and after that I want to store them on database然后我想将这些数据存储在 4 个步骤中,直到付款完成,然后我想将它们存储在数据库中

these 4 steps contains:这 4 个步骤包含:

  1. select product选择产品
  2. select accessories选择配件
  3. enter addresses输入地址
  4. enter payment detail输入付款明细

now please prefer your ways现在请更喜欢你的方式

which to use?使用哪个? cookie or session or something else ? cookie会话其他什么

I would use session but I want those data destroyed after 5 minutes and if I choose session lifetime to 5 minutes all of session data will destroy like my login session and I just want destroy the session that I created.我会使用会话,但我希望这些数据在 5 分钟后销毁,如果我选择会话生存期为 5 分钟,所有会话数据都将像我的登录会话一样销毁,我只想销毁我创建的会话。

The data you want to store temporarily is a cart quote data in terms of e-commerce.您要临时存储的数据是电子商务方面的购物车报价数据。 You can store this data in the database, with a timestamp column expires_at having value of current time + 5 minutes .您可以将此数据存储在数据库中,时间戳列expires_at值为current time + 5 minutes

Then in your model global scope, which will always filter queries and only get non-expired data :然后在您的模型全局范围内,它将始终过滤查询并仅获取未过期的数据:

/**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('expires_at', function (Builder $builder) {
            $builder->where('expires_at', '<=', Carbon::now());
        });
    }
  • The advantage of having this is, the data you can used for abandoned cart reports.这样做的好处是,您可以将数据用于废弃购物车报告。
  • In this data, mostly addresses, products will not contain all details but references using foreign keys to respective tables.在此数据中,主要是地址,产品将不包含所有详细信息,而是使用外键对相应表的引用。 If its not the case, I would recommend you to normalise your database.如果不是这种情况,我建议您规范化您的数据库。
  • Also, if you desire you can delete this data in a weekly cron to delete let's say all expired entries of last week.此外,如果您愿意,您可以在每周的 cron 中删除此数据,以删除上周所有过期的条目。

Sessions provide a way to store information about the user across multiple requests, you should check https://laravel.com/docs/5.7/session for me is the best option.In the session driver configuration option you can defines where session data will be stored for each request, for example cookie , were sessions are stored in secure, encrypted cookies.会话提供了一种跨多个请求存储用户信息的方法,您应该检查https://laravel.com/docs/5.7/session对我来说是最好的选择。在会话驱动程序配置选项中,您可以定义会话数据的位置为每个请求存储,例如cookie ,如果会话存储在安全的加密 cookie 中。 Then for your problem about destroy de data you can use the forget method will remove a piece of data from the session.然后,对于有关销毁数据的问题,您可以使用忘记方法将从会话中删除一段数据。 If you would like to remove all data from the session, you may use the flush method.如果您想从会话中删除所有数据,您可以使用flush 方法。

// Forget a single key...
  $request->session()->forget('key');
//or
  $request->session()->flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM