簡體   English   中英

當模型具有數據庫中不存在的臟屬性時,雄辯的更新方法將引發錯誤

[英]Eloquent update method throwing an error when a model has a dirty property that does not exist in the database

當添加模型中數據庫中不存在的屬性並且我運行update方法時,它將引發錯誤。 請參見下面的代碼作為示例。


>>> $foo = \App\Model::first()
=> App\Model {#4364
    "id": 1,
    "name": "John",
    "created_at": "2019-02-22 15:45:28",
}

>>> $foo->bar = 'bar';
=> "bar"

>>> $foo->update(['name' => 'new_name']);

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: 
Column not found: 1054 Unknown column 'bar' in 'field list' (SQL: 
update `models` set `name` = new_name, `updated_at` = 2019-02-22 
15:33:09, `bar` = bar where `id` = 1)'

預期的結果是,在foo上運行雄辯的update方法時,它將忽略屬性欄。 有誰知道為什么會這樣以及如何解決呢?

哦,好吧,當您致電:

$foo->bar = 'bar';

因此,當您現在調用$ foo時:

enter code here=> App\Model {#4364
    id: 1,
    name: "John",
    created_at: "2019-02-22 15:45:28",
    bar: "bar"
}

這會將bar添加到$ foo的上述實例中。 因此,當您對其調用update時,它將檢查所有列並確定要更新的列,在這種情況下,bar不是db中的列,因此會出現錯誤:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM