Код: Выделить всё
public function setRecentlyViewedCookie(Product $product): void
{
$cookieName = self::PRODUCT_RECENTLY_VIEWED_COOKIE_NAME;
$cookieValue = $this->getRecentlyViewedCookie();
$cookieValue[now()->timestamp] = $product->getKey();
$cookieValue = collect($cookieValue)
->unique()
->sortKeysDesc()
->take(self::PRODUCT_RECENTLY_VIEWED_COOKIE_QTY_LIMIT)
->toJson();
$minutes = 60 * 24 * self::PRODUCT_RECENTLY_VIEWED_COOKIE_TTL_DAYS;
$this->cookieFactory->queue(
$cookieName,
$cookieValue,
$minutes,
);
}
Когда я получаю этот массив из файла cookie, я делаю:
Код: Выделить всё
public function getRecentlyViewedCookie(): array
{
$cookieName = self::PRODUCT_RECENTLY_VIEWED_COOKIE_NAME;
try {
$cookieValue = request()->cookie($cookieName);
$cookieValue = json_decode($cookieValue, true);
} catch (Exception) {
$cookieValue = [];
}
return (array) $cookieValue;
}
Код: Выделить всё
array:12 [▼ // app/Services/ProductService.php:221
1733159352 => "9d9e9b2b-0974-4577-b75e-60d647f88bbf"
1733159348 => "9d9e9b2a-fe8f-44f3-b383-2eaa3614456c"
1733159341 => "9d9e9b2a-fcf6-419c-9e4b-b7c3f4ec8757"
1733159331 => "9d9e9b2a-f60f-42e1-9371-f831b1a28593"
1733159328 => "9d9e9b2a-f422-4faa-baad-16d9a147aaaf"
1733159326 => "9d9e9b2a-f121-4a46-a85d-8e0021172c52"
1733159175 => "9d989879-ddfc-48be-a581-f41036133017"
1733159163 => "9d989878-c8e9-4b75-af30-799ec780cfa7"
1733159161 => "9d989878-c69f-4833-add1-12b7d0f43c1f"
1733159157 => "9d989878-c320-4579-b261-e97b15dc5227"
1733159153 => "9d989878-b9f0-485d-bca3-304b7ef38f83"
1733159148 => "9d989878-b64c-49b3-92f4-fd8b01e84dcb"
]
Есть какие-нибудь подсказки, как упорядочить выборку продуктов с использованием этих временных меток?
Подробнее здесь: https://stackoverflow.com/questions/792 ... intain-ord
Мобильная версия