如何修复Hmset Redis

时间:2019-06-01 11:51:50

标签: php laravel redis

我正在开发一个apirest,我想使用Redis进行优化。 我使用hmset(键,值)进行遍历,并在端点中查找要更新的值。

我正在使用Lumen / Laravel和php 7.3

$package = Package::where('published', 1)
            ->where('persist', 1)
            ->get();
Redis::hmset('packages', array($package));

$packages = Redis::hgetall('packages');
return response()->json($packages, 200);

我需要获取json格式,但我知道了。我尝试了json_decode,但不起作用

[
    "[{\"id\":1,\"name\":\"Paquete A\",\"dimensions\":\"100x100x100\",\"weight_from\":\"0.00\",\"weight_to\":\"1.00\",\"detail\":\"forrado con papel madera\",\"price\":\"50.00\",\"distance_from\":\"0.00\",\"distance_to\":\"5.00\",\"exceed_price\":\"5.00\",\"status\":1,\"published\":1,\"persist\":1,\"created_at\":\"2019-05-29 01:35:53\",\"updated_at\":\"2019-05-29 01:35:53\"}]"
]

1 个答案:

答案 0 :(得分:0)

您需要在json_decode()之前使用stripslashes()。 Stripslashes()取消引号字符串的引用

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

它将删除斜线。您的代码将变成这样;

$package = Package::where('published', 1)
            ->where('persist', 1)
            ->get();
Redis::hmset('packages', array($package));

$packages = Redis::hgetall('packages');
return response()->json(stripslashes($packages), 200);