您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何为Laravel / Eloquent模型设置默认属性值?

如何为Laravel / Eloquent模型设置默认属性值?

这就是我现在正在做的:

protected $defaults = array(
   'quantity' => 9,
);

public function __construct(array $attributes = array())
{
    $this->setRawAttributes($this->defaults, true);
    parent::__construct($attributes);
}

我将其建议为PR,因此我们无需在每个Model上都声明此构造函数,并且只需$defaults在模型中声明数组即可轻松应用…

正如cmfolio指出的那样, :

只需覆盖$attributes属性!像这样:

protected $attributes = array(
   'quantity' => 9,
);

这里讨论了这个问题。

其他 2022/1/1 18:22:15 有215人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶