[PHP] Laravel cast array 数据库存 json 时的 unicode 编码问题

通过llama.cpp与羊驼聊天的网页界面- 详解 Serge 的启动使用

 

在模型上设置 accessor 和 mutator,将数组转为 json,并设置 json 选项。

class User extends Model
{

    public function setOptionAttribute($option)
    {
        $this->attributes['option'] = json_encode($option, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

    public function getOptionAttribute($value)
    {
         return json_decode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}

 

Refer:Laravel_unicode编码问题

doc: https://laravel.com/docs/8.x/eloquent-mutators

Link:https://www.cnblogs.com/farwish/p/13764108.html

posted on 2020-10-03 14:04  ercom  阅读(963)  评论(0编辑  收藏  举报