Laravel 9 Eloquent 模型 - 模型中保留的列名稱
P粉309989673
P粉309989673 2023-09-03 14:36:02
0
1
407
<p>我有一個 postgres 資料庫,其中有一個表,其中有一列名為「attributes」。 </p> <p>屬性列是 jsonb 類型,所以我使用 Eloquent 轉換:</p> <pre class="lang-php prettyprint-override"><code>protected $casts = [ 'attributes' => AsArrayObject::class, ]; </code></pre> <p>這似乎會導致問題,因為「屬性」已經是 Eloquent 模型屬性,似乎沒有任何別名列名稱的規定。 </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 模型屬性 - 而不是我的資料庫表中的“attributes”字段,從而導致以下錯誤:</p> <pre class="brush:php;toolbar:false;">SQLSTATE[42703]: Undefined column: 7 ERROR: column "a_property_of_the_attributes_jsonb_field" of relation "); LINE 1: update "mytable" set "a_property_of_the_attributes_jsonb_field" = $1 where "mypk" = ...</pre> <p>我無法重新命名該列,因為其他非 PHP 專案正在使用該資料庫。 </p> <p>如何將模型中的「屬性」欄位作為 ArrayObject 存取? </p>
P粉309989673
P粉309989673

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!