国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Laravel 9 Eloquent 模型 - 模型中保留的列名稱
P粉309989673
P粉309989673 2023-09-03 14:36:02
0
1
729
<p>我有一個 postgres 數(shù)據(jù)庫,其中有一個表,其中有一列名為“attributes”。</p> <p>屬性列是 jsonb 類型,因此我使用 Eloquent 轉換:</p> <pre class="lang-php prettyprint-override"><code>protected $casts = [ 'attributes' => AsArrayObject::class, ]; </code></pre> <p>這似乎會導致問題,因為“屬性”已經是 Eloquent 模型屬性,并且似乎沒有任何別名列名稱的規(guī)定。</p> <p>所以這一行:</p> <pre class="lang-php prettyprint-override"><code>$this->attributes['a_property_of_the_attributes_jsonb_field'] = 'hello word!'; </code></pre> <p>似乎正在訪問內部 Eloquent 模型屬性 - 而不是我的數(shù)據(jù)庫表中的“attributes”字段,從而導致以下錯誤:</p> <pre class="brush:php;toolbar:false;">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" = ...</pre> <p>我無法重命名該列,因為其他非 PHP 項目正在使用該數(shù)據(jù)庫。</p> <p>如何將模型中的“屬性”字段作為 ArrayObject 進行訪問?</p>
P粉309989673
P粉309989673

全部回復(1)
P粉034571623

簡單的解決方法是使用 ArrayObject 訪問器方法:

$this['attributes'] = ['x' => 'y'];
$stuff = $this['attributes'];

注意:仍然需要定義“attributes”轉換才能以數(shù)組形式訪問底層 jsonb 字段:

protected $casts = [
    'attributes' => AsArrayObject::class
];
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板