Change created_at and updated_at names to other fields

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.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1056 lessons, total 44 h 09 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials