当前位置: 首页 > news >正文

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

目录

    • 问题描述
    • 问题解决
      • 方式一:自定义属性
      • 方式二:继承覆写
      • 方式三:trait复用
      • 方式四:定义Cast子类
    • update不生效
    • 参考文章

问题描述

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'];
}

方式四:定义Cast子类

<?phpnamespace App\Casts;use Illuminate\Contracts\Database\Eloquent\CastsAttributes;class JsonCast implements CastsAttributes
{public function get($model, string $key, $value, array $attributes){return json_decode($value, true);}public function set($model, string $key, $value, array $attributes){return json_encode($value, JSON_UNESCAPED_UNICODE);}
}

使用

<?phpnamespace App\Models;use App\Casts\JsonCast;
use Illuminate\Database\Eloquent\Casts\Attribute;class UserModel extends Model
{protected $table = 'tb_user';protected $casts = ['alias'            => JsonCast::class,];
}

update不生效

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

平常更新数据是这样的

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

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

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

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

参考文章

  • Laravel5Model$catstoarrayutf-8JSON_UNESCAPED_UNICODE
  • PHP Laravel cast array 数据库存 json 时的 unicode 编码问题
  • Laravel使用Casts转换类型
  • laravel模型事件-update触发updating和updated的问题
http://www.lryc.cn/news/33694.html

相关文章:

  • 自动化测试总结--断言
  • 传输线的物理基础(三):传输线的瞬时阻抗
  • 第六章:多线程
  • 铁路与公路
  • GitHub Copilot 全新升级,工作效率提升 55%
  • 【IoT】《天道》中音响案例的SWOT分析
  • 如何实现接口幂等性
  • 相恨见晚的office办公神器(不坑盒子/打工人Excel插件2023年最新版)
  • matlab基础到实战(1)
  • 谷歌发布编写分布式应用的框架Service Weaver
  • 详解FPGA:人工智能时代的驱动引擎观后感
  • Rest/Restful接口
  • 【vue init】三.项目引入axios、申明全局变量、设置跨域
  • 搭建nextcloud私有云盘
  • 05 | 如何安全、快速地接入OAuth 2.0?
  • nest.js学习笔记(一)
  • win下载配置CIC Flowmeter环境并提取流量特征
  • 【LeetCode刷题-Java/Python】二分查找
  • Linux 6.2 已正式发布
  • Kubernetes 101,第一部分,基础知识
  • 企业级信息系统开发学习笔记1.7 基于XML配置方式使用Spring MVC
  • java反射,动态代理
  • React(六):Redux的使用、react-redux简化代码、redux模块化、RTK的使用
  • 静态库和动态库的打包与使用
  • h264编码之SPS解析
  • 使用R语言包clusterProfiler做KEGG富集分析时出现的错误及解决方法
  • 框架——MyBatis的入门案例
  • hadoop兼容性验证
  • 运维提质增效,有哪些办法可以做
  • c++基础——结构体