我有一个 postgres 数据库,其中有一个表,其中有一列名为“attributes”。
属性列是 jsonb 类型,因此我使用 Eloquent 转换:
protected $casts = [
'attributes' => AsArrayObject::class,
];
这似乎会导致问题,因为“属性”已经是 Eloquent 模型属性,并且似乎没有任何别名列名称的规定。
所以这一行:
$this->attributes['a_property_of_the_attributes_jsonb_field'] = 'hello word!';
似乎正在访问内部 Eloquent 模型属性 - 而不是我的数据库表中的“attributes”字段,从而导致以下错误:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "a_property_of_the_attributes_jsonb_field" of relation "mytable" does not exist LINE 1: update "mytable" set "a_property_of_the_attributes_jsonb_field" = $1 where "mypk" = ...
我无法重命名该列,因为其他非 PHP 项目正在使用该数据库。
如何将模型中的“属性”字段作为 ArrayObject 进行访问?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
简单的解决方法是使用 ArrayObject 访问器方法:
注意:仍然需要定义“attributes”转换才能以数组形式访问底层 jsonb 字段:
protected $casts = [ 'attributes' => AsArrayObject::class ];