# attributes vs #original in Laravel when dd()

uqdfh47h  于 2023-06-24  发布在  其他
关注(0)|答案(2)|浏览(114)

在我的Laravel代码中,当我在代码中的某个地方处理记录时,我会向数组添加新的属性(例如:$obj->newAttr=someContent),稍后当我dd()对象时,它显示了两个数组:#attributes和#original,但新创建的属性仅显示在#attributes数组中。
下面是一个例子,当我dd()对象时,我得到了#attributes数组:

  1. #attributes: array:21 [▼
  2. "field_name" => "229"
  3. "company_name" => "Maya Dairy"
  4. "seeding_rate" => 115
  5. "id" => 11
  6. "property_id" => 71
  7. "acreage" => "73.80"
  8. "status" => "current"
  9. "dairy_crop_variety_id" => 19
  10. "field_weight" => "1432.39"
  11. "moisture" => "67.00"
  12. "starch" => null
  13. "yield_id" => 50
  14. "crop" => "Wheat: <strong>Sumit 515</strong> @ <strong>21.3 T/c</strong>"
  15. "variety" => "Sumit 515"
  16. "planted_at" => "Nov 10, 2016"
  17. "harvested_at" => "May 1, 2017"
  18. "crop_age" => 172
  19. "cropDateLink" => "Harvested: <a href="#" data-toggle="modal" data-target="#editRowCrop" data-id="11" data-method="PATCH"><strong>May 1, 2017</strong> (172 days)</a>"
  20. "yield" => 21.3
  21. "inputs" => ""
  22. "files" => ""

原始数组:

  1. #original: array:16 [▼
  2. "field_name" => "229"
  3. "company_name" => "Maya Dairy"
  4. "seeding_rate" => 115
  5. "id" => 11
  6. "property_id" => 71
  7. "acreage" => "73.80"
  8. "status" => "current"
  9. "dairy_crop_variety_id" => 19
  10. "field_weight" => "1432.39"
  11. "moisture" => "67.00"
  12. "starch" => null
  13. "yield_id" => 50
  14. "crop" => "Wheat"
  15. "variety" => "Sumit 515"
  16. "planted_at" => "2016-11-10"
  17. "harvested_at" => "2017-05-01"

这两个数组有什么区别?有时新创建的属性在视图中不可用。我不知道为什么。任何建议都非常感谢。

ozxc1zmp

ozxc1zmp1#

$attributes包含模型属性的当前版本。$original应该包含给定对象的原始值,所以在这里你可以找到创建对象时使用的值,或者从数据库加载该对象时使用的值。
注意:****$原始值足够聪明,一旦你将模型保存到数据库中,就会用新的值更新-这个数组应该反映数据库中对象的数据。

beq87vna

beq87vna2#

我遇到的一些问题是:
$original显示了pivot属性,如果你有多对多的关系。但是,$attributes不显示它们。

相关问题