Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default value of an attribute on a Laravel model [duplicate]

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?

like image 690
Zeeshan Bin Iqbal Avatar asked Oct 07 '16 08:10

Zeeshan Bin Iqbal


1 Answers

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.) How to set a default attribute value for a Laravel / Eloquent model?

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


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/automatically-format-laravel-database-fields-with-accessors-and-mutators

3.) Universal accessors and mutators in Laravel 4

like image 155
Manish Avatar answered Nov 02 '22 11:11

Manish