Laravel 9 Eloquent 模型 - 模型中保留的列名称
P粉309989673
P粉309989673 2023-09-03 14:36:02
0
1
408
<p>我有一个 postgres 数据库,其中有一个表,其中有一列名为“attributes”。</p> <p>属性列是 jsonb 类型,因此我使用 Eloquent 转换:</p> <pre class="lang-php prettyprint-override"><code>protected $casts = [ 'attributes' =&gt; AsArrayObject::class, ]; </code></pre> <p>这似乎会导致问题,因为“属性”已经是 Eloquent 模型属性,并且似乎没有任何别名列名称的规定。</p> <p>所以这一行:</p> <pre class="lang-php prettyprint-override"><code>$this-&gt;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 &quot;a_property_of_the_attributes_jsonb_field&quot; of relation &quot;mytable&quot; does not exist LINE 1: update &quot;mytable&quot; set &quot;a_property_of_the_attributes_jsonb_field&quot; = $1 where &quot;mypk&quot; = ...</pre> <p>我无法重命名该列,因为其他非 PHP 项目正在使用该数据库。</p> <p>如何将模型中的“属性”字段作为 ArrayObject 进行访问?</p>
P粉309989673
P粉309989673

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!