如何访问对象属性?

How to access to object property?

MyObject {#300 ▼
  +dataType: DataType {#323 ▶}
  +data: Course {#328 ▼
    #connection: "mysql"
    #table: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:32 [▶]
    #original: array:32 [▶]
    #changes: array:2 [▼
      "prop" => 1
      "updated_at" => "2018-08-08 11:50:39"
    ]
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▶]
  }
}

我使用

在屏幕上打印这个对象
dd($args->data);

现在我需要检查是否有内部变化 然后我尝试访问 属性 与:

dd($args->data->changes);

dd($args->data['changes']);

但是我得到的始终是 NULL,而不是您在上面看到的数组。 我怎样才能访问 "changes"?

试试这个:

$args->data->getChanges();

用 foreach 循环试试

foreach($args as $arguslist){
$mainarray = $argulist->changes;
$arrayattr = $argulist->changes->prop;
}

首先,检查对象是否存在或not.If对象是否存在,然后尝试访问它的key。所以,代码如下:

$changes = $args->data ? $args->data->changes : [];
if(count($changes)>0){
 // iterate through the `$changes` to make changes 
}

您的数据似乎是 Eloquent 模型。 changes 属性 是模型 class 的 受保护 成员。因此,您无法从外部访问它。

dd函数的输出中,public成员以+字符为前缀,受保护成员以#字符为前缀。您可以查看这些前缀以了解您要访问的 属性 的可见性。

要获取 Eloquent 模型的更改,请使用 getChanges() 方法。在您的情况下,您应该编写如下代码:

dd($args->data->getChanges());