Laravel cast array json数据存数据库时unicode 编码问题和update更新不触发数据转换

小天天天天    PHP    999+ 次    2023-06-30 17:00:36


Model示例:

class UserModel extends Model
{
    protected $table = 'tb_user';

    protected $casts = [
        'alias'            => 'array'
    ];
}

直接存alias 字段,数据库会显示unicode码

["\u80c3\u75db\u554a"]

问题解决:

方式一:自定义属性:

class UserModel extends Model
{

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

方式二:继承覆写:

class UserModel extends Model
{
    protected $table = 'tb_user';

    protected $casts = [
        'alias'            => 'array'
    ];

    // 覆盖asJson方法
    protected function asJson($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }
}

方式三:trait复用:

trait UnicodeJsonTrait
{
    /**
     * 序列化json
     * @param $value
     * @return false|string
     */
    protected function asJson($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }
}

直接在基类里使用(也可以在基类中覆写)

class BaseModel extends Model
{
    use UnicodeJsonTrait;
}

继承基类

class UserModel extends BaseModel
{
    protected $table = 'tb_user';

    protected $casts = [
        'alias'            => 'array'
    ];
}


update不生效

save/create可以正常触发数据转换,update的时候需要注意

平常更新数据是这样的

$this->where(xxx)->update(xxx)

需要注意的是,这样写不会触发updating和updated事件

需要先获取模型再进行对应的操作,才能触发对应的模型事件

$this->where(xxx)->first()->update(xxx)

// 或
$this->find(xxx)->update(xxx)

如果你觉得本篇文章对您有帮助,请打赏作者

上一篇: 微信小程序个人中心模板

下一篇: 在Laravel外独立使用Eloquent

最新评论

暂无评论

热门文章

最新评论

网站数据

网站文章数:481

今日UV/PV/IP:12/13/12

昨日UV/PV/IP:22/28 /22

TOP