Answer the question
In order to leave comments, you need to log in
How to execute a query in migration after adding a field of the DateTime type, fill it with the value from created_at?
Hello. I am learning Laravel. I'm doing a blog. There is a table with posts. I made a migration in which I add a new field published_at of DateTime type.
$table->dateTime('published_at')->after('status')->nullable();
DB::raw('UPDATE `news` SET `published_at`=`created_at`');
$table->dateTime('published_at')->nullable(false)->change();
Answer the question
In order to leave comments, you need to log in
Make a field mutator in the model by writing the current timestamp from Carbon there:
public function setPublishedAtAttribute($value)
{
$this->attributes['published_at'] = ( ! empty($value)) ? $value : Carbon::now();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question