dd() 时 Laravel 中的 #attributes 与 #original

#attributes vs #original in Laravel when dd()(dd() 时 Laravel 中的 #attributes 与 #original)
本文介绍了dd() 时 Laravel 中的 #attributes 与 #original的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why can#39;t I update data in an array with foreach loop?(为什么我不能用 foreach 循环更新数组中的数据?)
Foreach for arrays inside of an array(Foreach 用于数组内的数组)
PHP array get next key/value in foreach()(PHP 数组在 foreach() 中获取下一个键/值)
Using preg_match on a multidimensional array to return key values arrays(在多维数组上使用 preg_match 返回键值数组)
php foreach as key, every two number as a group(php foreach 为key,每两个数字为一组)
Treat a PHP class that implements Iterator as an array(将实现 Iterator 的 PHP 类视为数组)