How do I update the column attribute of created_at in Laravel

Viewed 507

I want to add a precision of my User's created_at column. So I created another migration file to update the attribute of created_at but it doesn't work.

Here's my migration file

 /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dateTime('created_at', 3)->change();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('created_at');
    });
}

There's no error when I run the php artisan migrate. But whenever I try to create a new User it doesn't reflect changes.

I also added the date format in my User model

protected $dateFormat = 'Y-m-d H:i:s.v';
1 Answers

In the User model, add the 'created_at' column to the $fillable property on the model.

protected $fillable = ['name', 'created_at'];

When you save/update the user, Laravel will automatically update the 'created_at' field. Also, the correct date format for timestamp columns is 'Y-m-d H:i:s'.

Related