如何在Laravel模型上设置属性的默认值

如何在Laravel模型上设置属性的默认值

问题描述:

如何在Laravel模型上设置属性的默认值?

How to set the default value of an attribute on a Laravel model?

在创建迁移时应该设置默认值还是应该在模型类中设置它?

Should I set the default when creating a migration or should I set it in the model class?

您还可以在模型"中设置默认属性>

You can set Default attribute in Model also>

protected $attributes = [
        'status' => self::STATUS_UNCONFIRMED,
        'role_id' => self::ROLE_PUBLISHER,
    ];

您可以在这些链接中找到详细信息

You can find the details in these links

1.)如何设置Laravel/雄辩模型的默认属性值?

2.) https://laracasts.com/index.php/discuss/channels/eloquent/eloquent-help-generating-attribute-values-before-creating-record

您还可以使用存取器和放大器;变种人 您可以在Laravel文档中找到详细信息 1.) https://laravel.com/docs/4.2/eloquent#accessors-和变体

You can also Use Accessors & Mutators for this You can find the details in the Laravel documentation 1.) https://laravel.com/docs/4.2/eloquent#accessors-and-mutators

2.) https://scotch .io/tutorials/自动格式化laravel-database-fields-with-accessors-and-mutators

3.) Laravel 4中的通用访问器和变异器