問題描述
在我的 Laravel 代碼中,當我處理代碼中某處的記錄時,我正在向數組添加新屬性(例如 $obj->newAttr=someContent),稍后當我 dd() 對象顯示我兩個數組:#attributes 和#original,但新創建的屬性只顯示在#attributes 數組中.
In my Laravel code, when I am processing the records somewhere in the code I am adding new attributes to the array (ex. $obj->newAttr=someContent), later when I dd() the object it shows me two arrays: #attributes and #original, but the newly created attribute show up only in #attributes array.
這是一個例子,當我 dd() 對象時,我得到了 #attributes 數組:
Here is an example when I dd() the object, I get the #attributes array:
#attributes: array:21 [▼
"field_name" => "229"
"company_name" => "Maya Dairy"
"seeding_rate" => 115
"id" => 11
"property_id" => 71
"acreage" => "73.80"
"status" => "current"
"dairy_crop_variety_id" => 19
"field_weight" => "1432.39"
"moisture" => "67.00"
"starch" => null
"yield_id" => 50
"crop" => "Wheat: <strong>Sumit 515</strong> @ <strong>21.3 T/c</strong>"
"variety" => "Sumit 515"
"planted_at" => "Nov 10, 2016"
"harvested_at" => "May 1, 2017"
"crop_age" => 172
"cropDateLink" => "Harvested: <a href="#" data-toggle="modal" data-target="#editRowCrop" data-id="11" data-method="PATCH"><strong>May 1, 2017</strong> (172 days)</a>"
"yield" => 21.3
"inputs" => ""
"files" => ""
和原始數組:
#original: array:16 [▼
"field_name" => "229"
"company_name" => "Maya Dairy"
"seeding_rate" => 115
"id" => 11
"property_id" => 71
"acreage" => "73.80"
"status" => "current"
"dairy_crop_variety_id" => 19
"field_weight" => "1432.39"
"moisture" => "67.00"
"starch" => null
"yield_id" => 50
"crop" => "Wheat"
"variety" => "Sumit 515"
"planted_at" => "2016-11-10"
"harvested_at" => "2017-05-01"
這些數組有什么區別?有時新創建的屬性在視圖中不可用.我不知道為什么.任何建議都非常感謝.
What is the difference between these arrays? And sometimes the newly created attributes are not available in view. I am not sure why. Any suggestions are highly appreciated.
推薦答案
$attributes 包含模型屬性的當前版本.$original 應該包含給定對象的原始值,因此在這里您將找到創建對象時使用的值或從數據庫加載的值.
$attributes contains the current version of model's attributes. $original is supposed to contain, well, the original values of given object, so here you'll find the values that object was created with or the values it was loaded with from the database.
注意:$original 值足夠聰明,可以在將模型保存到數據庫后用新值更新 - 這個想法是這個數組應該反映對象的數據庫中的數據.
Note: the $original values are clever enough to be updated with new values once you save the model to the database - the idea is that this array should reflect object's data in the database.
這篇關于dd() 時 Laravel 中的 #attributes 與 #original的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!