在我的 Laravel 代码中,当我在代码中某处处理记录时,我正在向数组添加新属性(例如 $obj->newAttr=someContent),稍后当我 dd() 对象时,它向我显示了两个数组:#attributes 和#original,但新创建的属性仅显示在#attributes 数组中。
这是一个例子,当我 dd() 对象时,我得到了#attributes 数组:
#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"
这些数组有什么区别?有时新创建的属性在 View 中不可用。我不确定为什么。非常感谢任何建议。
最佳答案
$attributes 包含模型属性的当前版本。 $original 应该包含给定对象的原始值,因此您可以在此处找到创建对象时使用的值或从数据库中加载的值。
注意:$original 值足够聪明,可以在将模型保存到数据库后用新值更新 - 这个想法是这个数组应该反射(reflect)对象的数据库中的数据。
关于php - dd() 时 Laravel 中的#attributes vs #original,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47126882/