如何在Laravel模型上设置属性的默认值 [英] How to set the default value of an attribute on a Laravel model

查看:2295
本文介绍了如何在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中的通用访问器和变异器

这篇关于如何在Laravel模型上设置属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆