Skip to main content
Tutorial Free

Change created_at and updated_at names to other fields

September 19, 2017
1 min read
Let's imagine a scenario where you have an old database and re-writing codebase to Laravel. Database structure may not follow Laravel standards - created_at and updated_at fields are named differently. How can you "tell it to Laravel"? For example, if your fields are called create_time and update_time, here's what you do in migration:
Schema::create('movies', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamp('create_time')->nullable();
    $table->timestamp('update_time')->nullable();
});
And then, apparently, it's pretty simple - you can override the names of these fields in your model.
class Movie extends Model
{
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
}
And here you don't need any more mutators or attributes - these two fields will work exactly with the same auto-fill and auto-update logic as Laravel's native ones.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses on Laravel Daily

Laravel 13 Starter Kit Teams and Customizations

10 lessons
33 min

Laravel 13 Eloquent: Expert Level

41 lessons
1 h 34 min

Queues in Laravel 13

18 lessons
1 h 12 min read

No comments yet…