1

I'm trying to rename the timestamps columns (created_at and updated_at) of the user table in my database. I have already seen this answer but when I override the CREATED_AT and UPDATED_AD constants like that:

class User extends Authenticatable
{
    const CREATED_AT = 'user_creation_date';
    const UPDATED_AT = 'user_update_date';
    ...
}

all it does is rename the properties of the User model, i.e. $user->user_creation_date and $user->user_update_date. The database columns remain unchanged. How should I do to rename the columns of my database while keeping the auto-update feature?

Thank you for your help.

JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
  • 2
    Pragmatic question: why would you want this? a `created_at` stamp on your `user` is pretty obvious in indicating that a User record was created at that time. Keeping those column names consistent in all your tables also allows for easier querying later on. – Loek May 09 '18 at 08:35
  • That's just more convenient when doing junctures in SQL queries, instead of doing `table1.updated_at = table2.updated_at` you just do `table1_update_date = table2_update_date`. – JacopoStanchi May 09 '18 at 08:38
  • That's literally 1 character difference, right? Anyway, alistaircol's answer is probably the best one. – Loek May 09 '18 at 08:45
  • Yes I will test it. As of what you said, that's not a matter of characters difference, but a matter of handling by the framework. It's way easier to do a `->where('table1_column','table2_column')` than a `DB::raw('table1.column = table2.column')`. I don't choose what my boss tells me to do. – JacopoStanchi May 09 '18 at 08:52
  • `DB::table('table1')->join('table2', 'table1.created_at', '=', 'table2_created_at')->select('*')->get()`? (Or a left join: `DB::table('table1')->leftJoin('table2', 'table1.created_at', '=', 'table2_created_at')->get()`) Not trying to get in the way, you probably have good reasons! I'm just having a hard time to see what exactly the gain is of solving your question ;) – Loek May 09 '18 at 08:59

2 Answers2

5

You will need to update your users table migration file in database/migrations, it will be a file like 2014_10_12_000000_create_users_table.php.

You likely have $table->timestamps(); in the Schema::create call.

Looking at the code for timestamp() in vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php it reveals:

public function timestamps($precision = 0)
{
    $this->timestamp('created_at', $precision)->nullable();

    $this->timestamp('updated_at', $precision)->nullable();
}

So:

Schema::create('users', function (Blueprint $table) {
  // ..
  $table->timestamps();
});

Remove the call to $table->timestamps(); and add add the two columns you want to call the timestamps:

Schema::create('users', function (Blueprint $table) {
  // ..
  $this->timestamp('user_creation_date', 0)->nullable();
  $this->timestamp('user_update_date', 0)->nullable();
});

You will need to run migration again, make sure you back up data as this will drop table and recreate them.

Hope this helps.

alistaircol
  • 1,433
  • 12
  • 22
2

You can use get attributes, e.g.

class User extends Authenticatable
{
    protected $timestamps = true;
    protected $hidden = ['created_at', 'updated_at']; 
    protected $appends = ['user_creation_date', 'user_update_date']; 
    public function getUserCreationDateAttribute(){
        return $this->created_at; 
    }
    public function getUserUpdateDateAttribute(){
        return $this->updated_at; 
    }
}

Now you will get both of the columns created_at and updated_at data in the fields user_creation_date and user_update_date. The fields created_at and updated_at will stay hidden when you return array or json response or convert object to array or json.

Dev
  • 6,570
  • 10
  • 66
  • 112