【发布时间】:2021-01-01 10:00:22
【问题描述】:
我正在使用 Laravel 处理会话数据,我可以将会话存储到变量中并检索整个数据。但我的问题是我想从存储的会话中将会话订购为最新的 5 个 id。
存储会话
session()->push('products.recently_viewed', $product->getKey());
检索会话
$ids = session()->get('products.recently_viewed');
$recently_viewed = Product::whereIn('id', $ids)->get();
检索工作正常,我只想从会话products.recently_viewed 中获取最新的 5 个 id
例如
来自会话的数据
[1,3,7,6,8,9,11,2]
我想获取的数据
[2,11,9,8,6]
【问题讨论】: