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

在模型上设置 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);
    }
}

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

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

原文地址:https://www.cnblogs.com/farwish/p/13764108.html