【问题标题】:How to access properties on a facade in php如何在 php 中访问外观上的属性
【发布时间】:2021-01-26 23:37:47
【问题描述】:

当我转储这个Customer::where('email', '=', $inputObj['email']-first(); 时,输出有一些属性。其中一些有#,另一些有+。例如,一个是+exists: true,我可以通过->exists() 访问它,它返回给我true。另一个是#attributes: array:10 [...],我可以看到它是一个关联数组,其值在dump 中,但我不能像->attributes 或像这样['attributes'] 那样访问。不同的符号是什么意思?如何访问 attributes 属性中的值?

我真的很想深入了解正在发生的事情,因此感谢任何有启发性的 cmets (:

【问题讨论】:

  • privateprotected 属性。

标签: php laravel model facade


【解决方案1】:

这些符号用于可见性,请参考:

https://stackoverflow.com/a/4361582/8637968

您不能访问“属性”,因为它是私有属性,请记住 Laravel 中的 var_dump() 或 dd()(代表转储和死亡)旨在用于调试目的,这就是您看到私有的原因属性,但私有属性只能从类内部访问,在 Laravel 的情况下,您可以访问“属性”数组中的属性作为属性本身,例如:假设对于您的客户模型,您有一个名为“ name”,该列将在“attributes”数组中,您可以像这样访问它:

$customer = Customer::where('email', '=', 'some_email@mail.com')->first();
$customer->name; // assuming that the model exists, i.e: if ($customer != null) {}

【讨论】:

  • 非常感谢您的帮助!
猜你喜欢
  • 2014-09-15
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
相关资源
最近更新 更多