如何在 Laravel 模型上设置属性的默认值?
我应该在创建迁移时设置默认值还是应该在模型类中设置它?
您还可以在模型中设置默认属性>
protected $attributes = [
'status' => self::STATUS_UNCONFIRMED,
'role_id' => self::ROLE_PUBLISHER,
];
您可以在这些链接中找到详细信息
1.) 如何为 Laravel / Eloquent 模型设置默认属性值? https://stackoverflow.com/questions/18747500/how-to-set-a-default-attribute-value-for-a-laravel-eloquent-model
2.) https://laracasts.com/index.php/discuss/channels/eloquent/eloquent-help-generate-attribute-values-before-creating-record 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-and-mutators https://laravel.com/docs/4.2/eloquent#accessors-and-mutators
2.) https://scotch.io/tutorials/automatically-format-laravel-database-fields-with-accessors-and-mutators https://scotch.io/tutorials/automatically-format-laravel-database-fields-with-accessors-and-mutators
3.) Laravel 4 中的通用访问器和修改器 https://stackoverflow.com/questions/22482310/universal-accessors-and-mutators-in-laravel-4
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)