Change name of Laravel's created_at and updated_at

Can I map Laravel's timestamps from:

created_at to post_date and post_date_gmt?

updated_at to post_modified and post_modified_gmt?


I'm migrating a Wordpress application to Laravel.

I'm accessing a copy of the Wordpress database using Laravel. The live version of the database is still in use so I don't want to change the schema.

The Posts table has post_date, post_date_gmt, post_modified and post_modified_gmt, but Laravel is expecting created_at and updated_at.

Is there anyway to change the column names that Laravel looks for?

I'd like Laravel to update the timestamps of all the columns that are already there.

The accepted answer may cause problems with updating timestamps unfortunately.

You'd better override consts on your model:

const CREATED_AT = 'post_date';
const UPDATED_AT = 'post_modified';

then methods getCreatedAtColumn and getUpdatedAtColumn will return post_date and post_modified respectively, but won't do any harm.

For the other columns you need use events like @Oni suggested.

Configuration - Laravel, You could use the following command to rename your application php artisan app:name CodeTube. Laravel Manual. Looking at php artisan you should have a php artisan app:name NewNamespace command to change the namespace. Make sure that you are on the latest laravel version. Old answer. To change the namespace of your app you have to edit the composer.json file:

If you look into the source of the Eloquent class

https://github.com/illuminate/database/blob/4.2/Eloquent/Model.php#L223-L235

You should be able to change these column names pretty easily by overriding those constants.

<?php

class YourModel extends Eloquent {

    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'post_date';

    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'post_modified';

}

As for the _gmt version of those timestamps, you might want to look into events. Here is a good start

http://driesvints.com/blog/using-laravel-4-model-events

How to change application name in laravel 5.8, The app:name command, no longer available in laravel 6.0, there is some way to change the namespaces, as was being done with that� Its easy to change the table column name or datatype in laravel. Here we have a projects table where the column name is “notes” and we change it note to “description” with creating a new migration and rename the column name easily.

You could try using the attribute getters and getters:

class Post extends Eloquent
{
    public function getCreatedAtAttribute()
    {
        return $this->attributes['post_date'];
    }

    public function setCreatedAtAttribute($value)
    {
        $this->attributes['post_date'] = $value;

        // this may not work, depends if it's a Carbon instance, and may also break the above - you may have to clone the instance
        $this->attributes['post_date_gmt'] = $value->setTimezone('UTC');
    }
}

I don't know if this will work, but you could give it a go. Certainly a base to go on maybe - you'll have to play around with it.

[6.0] Where is the command app:name? 🥺 � Issue #29810 � laravel , Hello, I just started with Laravel and I am a bit lost with the initial setup. I am trying to change the app name in the blade templates as: {{ confi Laravel framework comes with built-in Auth mechanism for logging-in and registering users. As a part of that we have starting migration files, one of them is creating a main Auth DB table called users. But what if we want to change that table name, and use Users for some other purpose? It's actually pretty easy to do.

Just put this in your tables model file,

 const CREATED_AT = 'your_custom_created_at_field_name';
 const UPDATED_AT = 'your_custom_updated_at_field_name';

My model looks like this for avoiding confusions

class diarymodule extends Model
{
    protected $table = 'diarymodule';
    const CREATED_AT = 'CreatedDate';
    const UPDATED_AT = 'UpdatedDate';
}

App name can only be changed in the .env, I'm using config('app.name') on my html to set the title of my websi If you'd want to change your app namespace for your code so� Table Names. Note that we did not tell Eloquent which table to use for our Flight model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table.

Laravel config application name, But if the APP_NAME value is changed into a value contains space, the Laravel powered web-based app will display blank page. Below is the� The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

Change the name of the Laravel application, Get code examples like "laravel 7 change app name" instantly right from your google search results with the Grepper Chrome Extension. Hi guys I have renamed a model from Bookings.php to Booking.php to match the naming convention but now laravel cannot find the file. I receive this er

laravel 7 change app name Code Example, You can also change the table using migration. Schema::rename($ currentTableName, $newTableName);. If you want to remove an existing table� The default Laravel application structure is intended to provide a great starting point for both large and small applications. But you are free to organize your application however you like. Laravel imposes almost no restrictions on where any given class is located - as long as Composer can autoload the class.

Comments
  • Can you explain how the accepted answer may cause problems? I'm intrigued.
  • Eloquent Model class refers to static::CREATED_AT and static::UPDATED_AT in some methods, also in that particular 2 that @Oni overrides. So hardcoding the columns in those methods would make Eloquent search for created_at and updated_at anyway when updating timestamps (updateTimestamps method for example). The same goes for DELETED_AT when soft deletes are used.
  • Hmm that seems like a possible bug in Laravel (in that it should be using the methods not the constants), right?
  • If you override the consts, does that only affect the model that you set them in, or does it override the model it extends as well, therefore affecting all models?
  • Of course only the model you set them in AND all that inherit from it, but not parent class - Eloquent\Model.
  • Check my answer as this is going to lead to unexpected behaviour.
  • I've looked into the source again and it's as what deczo said, the code that I provide will result in error when it comes to other methods that use the static constants. I've updated the code example to reflect deczo's suggestion.
  • Oh ignore this, Oni's is much better!
  • how is this answer different from the accepted answer?