本文介绍了如何在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
您还可以使用存取器和放大器;变种人您可以在Laravel文档中找到详细信息1.) https://laravel.com/docs/4.2/eloquent#accessors-和变体
You can also Use Accessors & Mutators for thisYou can find the details in the Laravel documentation1.) https://laravel.com/docs/4.2/eloquent#accessors-and-mutators
2.) https://scotch .io/tutorials/自动格式化laravel-database-fields-with-accessors-and-mutators
这篇关于如何在Laravel模型上设置属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!