Reputation: 19
I want to set default value to the last day of year in laravel migration column, for example "31-12-2021" but I want year to be changed automatically every year
Upvotes: 0
Views: 415
Reputation: 1864
My understanding is that migration files are used to set table columns data types only, and the timestamps are supported data types inside the database.
So, you can't make a dynamic default value other than a timestamp.
I will suggest this logic inside your Model:
class User extends Eloquent {
public $timestamps = false;
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->created_at = //the calculation of the last day of the year
});
}
}
Edit: As @Andy commented you can get the last day of the year like:
$model->created_at = date("Y-m-d", strtotime("last day of december"));
Upvotes: 1