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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18747500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:09:02  来源:igfitidea点击:

How to set a default attribute value for a Laravel / Eloquent model?

phpmodellaravellaravel-4eloquent

提问by J. Bruni

If I try declaring a property, like this:

如果我尝试声明一个属性,如下所示:

public $quantity = 9;

...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I am blocking access to the actually real and existent "quantity" attribute.

...它不起作用,因为它不被视为“属性”,而只是模型类的属性。不仅如此,我还阻止访问实际真实存在的“数量”属性。

What should I do, then?

那我该怎么办?

回答by cmfolio

An update to this...

对此的更新...

@j-bruni submitted a proposal and Laravel 4.0.x is now supporting using the following:

@j-bruni 提交了一个提案,Laravel 4.0.x 现在支持使用以下内容:

protected $attributes = array(
  'subject' => 'A Post'
);

Which will automatically set your attribute subjectto A Postwhen you construct. You do not need to use the custom constructor he has mentioned in his answer.

它会自动设置你的属性subjectA Post当你构建。您不需要使用他在回答中提到的自定义构造函数。

However, if you do end up using the constructor like he has (which I needed to do in order to use Carbon::now()) be careful that $this->setRawAttributes()will override whatever you have set using the $attributesarray above. For example:

但是,如果您最终像他那样使用构造函数(我需要这样做才能使用Carbon::now()),请小心,这$this->setRawAttributes()将覆盖您使用$attributes上面的数组设置的任何内容。例如:

protected $attributes = array(
  'subject' => 'A Post'
);

public function __construct(array $attributes = array())
{
    $this->setRawAttributes(array(
      'end_date' => Carbon::now()->addDays(10)
    ), true);
    parent::__construct($attributes);
}

// Values after calling `new ModelName`

$model->subject; // null
$model->end_date; // Carbon date object

// To fix, be sure to `array_merge` previous values
public function __construct(array $attributes = array())
{
    $this->setRawAttributes(array_merge($this->attributes, array(
      'end_date' => Carbon::now()->addDays(10)
    )), true);
    parent::__construct($attributes);
}

See the Github threadfor more info.

有关更多信息,请参阅Github 线程

回答by J. Bruni

This is what I'm doing now:

这就是我现在正在做的:

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

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

I will suggest this as a PR so we don't need to declare this constructor at every Model, and can easily apply by simply declaring the $defaultsarray in our models...

我将建议将此作为 PR,因此我们不需要在每个模型中都声明此构造函数,并且只需$defaults在我们的模型中声明数组即可轻松应用...



UPDATE:

更新

As pointed by cmfolio, the actual ANSWER is quite simple:

正如 cmfolio 所指出的,实际的答案非常简单

Just override the $attributesproperty! Like this:

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

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

The issue was discussed here.

此处讨论了该问题。

回答by pbgneff

I know this is really old, but I just had this issue and was able to resolve this using this site.

我知道这真的很旧,但我只是遇到了这个问题并且能够使用这个网站解决这个问题。

Add this code to your model

将此代码添加到您的模型中

protected static function boot()
{
   parent::boot();

   static::creating(function ($model) {
        $model->user_id = auth()->id();
    });
}

Update/Disclaimer

更新/免责声明

This code works, but it will override the regular Eloquent Model creatingEvent

此代码有效,但它会覆盖常规的 Eloquent 模型creating事件