【问题标题】:How to define class fields for Eloquent model如何为 Eloquent 模型定义类字段
【发布时间】:2015-04-07 07:43:06
【问题描述】:

我从 Yii 和 Codeigniter 来到 Laravel。并且找不到如何为 Eloquent 模型描述类字段的解决方案。我想知道这个类有哪些可用的变量。问题是当我定义变量 public => 当我尝试使用它时,这个变量是空的。如果我在使用时定义变量 protected => 一切都很好,但是这个变量不能用于自动竞争(因为这个 var 是受保护的)。

谢谢!

class User extends Eloquent
{

...
protected $registered; // works, but not available
public $registered; // not works, always empty
var $registered; // not works, always empty
...

}

【问题讨论】:

    标签: eloquent laravel-5


    【解决方案1】:

    Eloquent 的属性使用内部 attributes 数组并依赖于神奇的 __get()__set() 方法。但是,当您定义变量时,它们永远不会被调用。要获得自动完成并记录属性,您只需将它们添加到 docblock 注释中即可:

    /**
     * @property bool $registered is the user registered?
     */
    class User extends Eloquent
    

    其实@property注解具体就是designed for magic properties

    @property 显示了在类中找到的“神奇”属性变量。

    【讨论】:

      猜你喜欢
      • 2014-11-11
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多