我正在建立一个在线商店网站,作为一个附带项目与Laravel5.3。我的目标是实现匿名和身份验证的购物。以下是到目前为止的Customer、Cart和CartItem表:
// (1) customers table...
table->increments('id');
$table->integer('user_id')->unsigned(); // -> User hasOne('App\Customer') rel.
$table->string('first_name');
// last_name, dob, phone, etc. + timestamps
// (2) carts table...
$table->increments('id');
// -> Customer identification (and not user_id, because admins don't have carts!):
$table->integer('customer_id')->unsigned()->nullable();
// -> Guest identification:
$table->string('session_id');
$table->timestamps();
// (3) cart_items table...
$table->increments('id');
$table->integer('cart_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('quantity')->unsigned();
$table->timestamps();但是,我不知道如何识别来宾用户并将他们绑定到手推车中。这样做的想法是,如果您以客人身份购物,然后注册或登录,您的购物车应该经过身份验证,也就是说,我需要做$cart->customer_id = Auth::user()->customer->id。但是,我如何将客人绑定到他们的匿名车(即当$cart->customer_id == null)?
方法
Session::getId();时将$cart->session_id存储在中,那么我就处于risk中,因为Laravel中的会话id是再生(例如在登录身份验证期间)。所以,如果客人登录,我就找不到他们的购物车Cart::where('session_id', Session::getId()),因为他们的Session id在登录时刚刚重新生成!session_id,例如在carts迁移中写入$table->uuid('guest_id')。此令牌将持久化在数据库和客户端cookie中(正如Snapey在关于燕麦的类似问题中所建议的)。User,可能是guest的角色,但没有密码或电子邮件。当客户决定注册时,我可以用注册表单中的值填充这些字段。注意事项
不过,我在cookie中看到的问题是,它们不那么“持久”,而且我仍然觉得在会话中存储购物车(包括匿名的和身份验证的)更安全。同时,我不能将会话lifetime更改为例如30天,因为会话也会存储身份验证信息,而且这可能对服务器来说太过了。至于来宾用户,我需要使email和password在users表中为空,这似乎是一种糟糕的做法(不要更改代码--您不拥有任何类型的东西)。
问题
是否有其他方法来解决这个问题?什么是最好和最安全的做法,以确定客人的用户,并将他们与他们的匿名购物车?最后,如果我要使用Laravel会话,那么使用database会话或者更确切地说使用file会话是否有意义?是否需要对customers、carts或cart_items表进行任何更改?
我知道Laravel的购物车包确实存在,但到目前为止,我还没有找到一个集成了匿名+认证购物和/或与users表集成的购物车包。
发布于 2021-09-23 02:10:11
嗯,我也面临着同样的问题,但我现在想到的最好的解决方案是给用户一个可下载的跟踪号码或类似的东西,这样当他们回来时,他们就可以输入该代码,并且我可以根据该代码验证购物车项目。
https://stackoverflow.com/questions/41201788
复制相似问题