php - About redis read and write escape
迷茫
迷茫 2017-07-03 11:40:42
0
2
1487

I only came into contact with redis and related things because of work needs. The result is as follows:
Part of the code is as follows
$newsRedis = Redis::get('news_'.$id);

    if ($newsRedis) return $newsRedis;
    $re = NewNotice::select('community', 'title', 'created_at', 'content', 'initiator', 'img')->where('id', '=', $id)->get();

    $change = NewNotice::where('id', '=', $id)->update([
        'state' => '1'
    ]);
    if (!$change) return 'NOTICE_ERROR';

// dd($change);

    Redis::set('news_'.$id,$re);
    Redis::expire('news_'.$id,10);
    return $re;
}

It turned out that there was an escape slash before each symbol

The following is the result of my direct return $re

Why does this happen? How can I avoid escaping?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
漂亮男人

You take it out, first json_decode and then encapsulate the data and output it to the front end.
Your escaping is because it was json_encode twice.

What you store in Redis is a JSON string. You take it out directly, put it into the data, and perform json_encode again, so it is normal for this to happen.

淡淡烟草味

This escaping is normal. It’s because of json. JSON requires that all key values ​​must be enclosed in double quotes ". Values ​​containing double quotes naturally need to be escaped. You dd($change->datas) will not have slashes If you really don’t want to see the double quotes, you can url_encode first and then url_decode when using it, but it’s not necessary

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!